--DO NOT MODIFY THIS FILE. IT IS ALWAYS OVERWRITTEN ON GENERATION.

--This SQL is generated for internal stored procedures for table [Ancestor]

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[gen_Ancestor_Delete]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
	drop procedure [dbo].[gen_Ancestor_Delete]
GO

CREATE PROCEDURE [dbo].[gen_Ancestor_Delete]
(
	@Original_AncestorId [Int]
)
AS
SET NOCOUNT OFF;

DELETE FROM
	[dbo].[Ancestor] 
WHERE 
	[AncestorId] = @Original_AncestorId;

if (@@RowCount = 0) return;


GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[gen_Ancestor_Insert]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
	drop procedure [dbo].[gen_Ancestor_Insert]
GO

CREATE PROCEDURE [dbo].[gen_Ancestor_Insert]
(
	@AncestorId [Int] = null,
	@Name [VarChar] (50),
	@CreatedDate [DateTime] = null,
	@CreatedBy [Varchar] (50) = null,
	@ModifiedBy [Varchar] (50) = null

)
AS
SET NOCOUNT OFF;

if (@CreatedDate IS NULL)
SET @CreatedDate = getdate()
DECLARE @ModifiedDate [DateTime]
SET @ModifiedDate = getdate()
IF (@AncestorId < 0) SET @AncestorId = NULL;
if ((@AncestorId IS NULL))
BEGIN
INSERT INTO [dbo].[Ancestor]
(
	[Name],
	[CreatedDate],
	[CreatedBy],
	[ModifiedDate],
	[ModifiedBy]
)
VALUES
(
	@Name,
	@CreatedDate,
	@CreatedBy,
	@ModifiedDate,
	@ModifiedBy
);

if (@@RowCount = 0) return;

END
ELSE
BEGIN
SET identity_insert [dbo].[Ancestor] on
INSERT INTO [dbo].[Ancestor]
(
	[AncestorId],
	[Name],
	[CreatedDate],
	[CreatedBy],
	[ModifiedDate],
	[ModifiedBy]
)
VALUES
(
	@AncestorId,
	@Name,
	@CreatedDate,
	@CreatedBy,
	@ModifiedDate,
	@ModifiedBy
);

if (@@RowCount = 0) return;

SET identity_insert [dbo].[Ancestor] off
END


SELECT 
	[dbo].[Ancestor].[ancestorid],
	[dbo].[Ancestor].[name],
	[dbo].[Ancestor].[CreatedBy],
	[dbo].[Ancestor].[CreatedDate],
	[dbo].[Ancestor].[ModifiedBy],
	[dbo].[Ancestor].[ModifiedDate],
	[dbo].[Ancestor].[Timestamp]

FROM
[dbo].[Ancestor]
WHERE
	[dbo].[Ancestor].[AncestorId] = SCOPE_IDENTITY();
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[gen_Ancestor__AUDIT_SELECT]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
	drop procedure [dbo].[gen_Ancestor__AUDIT_SELECT]
GO

CREATE PROCEDURE [dbo].[gen_Ancestor__AUDIT_SELECT]
(
 @AncestorId [Int],
 @__auditType [int] = null,
 @__pageOffset [int] = 1,
 @__recordsPerPage [int] = null,
 @__startDate [datetime] = null,
 @__endDate [datetime] = null
)
AS

SET NOCOUNT ON;

if(@__pageOffset < 1 ) SET @__pageOffset = 1
if((NOT (@__recordsPerPage IS NULL)) AND (@__recordsPerPage < 1 )) SET @__recordsPerPage = 1
IF (@__recordsPerPage IS NULL)
BEGIN
SELECT
	[__AUDIT__Ancestor].[__action],
	[__AUDIT__Ancestor].[__insertdate],
	[__AUDIT__Ancestor].[ModifiedBy],
	[__AUDIT__Ancestor].[AncestorId],
	[__AUDIT__Ancestor].[Name]
FROM
	[__AUDIT__Ancestor]
WHERE
	((@__auditType IS NULL) OR ([__AUDIT__Ancestor].__action = @__auditType)) AND
	((@__startDate IS NULL) OR ([__AUDIT__Ancestor].__insertdate >= @__startDate)) AND
	((@__endDate IS NULL) OR ([__AUDIT__Ancestor].__insertdate < @__endDate)) AND
	(@AncestorId = [__AUDIT__Ancestor].AncestorId)
	ORDER BY [__AUDIT__Ancestor].__insertdate DESC, [__AUDIT__Ancestor].__rowid DESC

END
ELSE
BEGIN
SELECT
	[__TempTable].[__action],
	[__TempTable].[__insertdate],
	[__TempTable].[ModifiedBy],
	[__TempTable].[AncestorId],
	[__TempTable].[Name]
FROM
	(SELECT  ROW_NUMBER() OVER (ORDER BY [__AUDIT__Ancestor].__insertdate DESC, [__AUDIT__Ancestor].__rowid DESC)
	AS __row,
	[__AUDIT__Ancestor].[__action],
	[__AUDIT__Ancestor].[__insertdate],
	[__AUDIT__Ancestor].ModifiedBy,
	[__AUDIT__Ancestor].[AncestorId],
	[__AUDIT__Ancestor].[Name]
	FROM 
		[__AUDIT__Ancestor]
	WHERE
		((@__auditType IS NULL) OR ([__AUDIT__Ancestor].__action = @__auditType)) AND
	((@__startDate IS NULL) OR ([__AUDIT__Ancestor].__insertdate >= @__startDate)) AND
	((@__endDate IS NULL) OR ([__AUDIT__Ancestor].__insertdate < @__endDate)) AND
		(@AncestorId = [__AUDIT__Ancestor].AncestorId)
	)
	AS [__TempTable]
WHERE
	__row >= (((@__pageOffset-1)*@__recordsPerPage)+1) AND __row <= (@__pageOffset*@__recordsPerPage)

END
return (select COUNT(*) 
	FROM [__AUDIT__Ancestor] WHERE
		((@__auditType IS NULL) OR ([__AUDIT__Ancestor].__action = @__auditType)) AND
	((@__startDate IS NULL) OR ([__AUDIT__Ancestor].__insertdate >= @__startDate)) AND
	((@__endDate IS NULL) OR ([__AUDIT__Ancestor].__insertdate < @__endDate)) AND
		(@AncestorId = [__AUDIT__Ancestor].AncestorId)
		)

GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[gen_Ancestor__AUDIT_SELECT_ALL]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
	drop procedure [dbo].[gen_Ancestor__AUDIT_SELECT_ALL]
GO

CREATE PROCEDURE [dbo].[gen_Ancestor__AUDIT_SELECT_ALL]
(
 @__auditType [int] = null,
 @__pageOffset [int] = 1,
 @__recordsPerPage [int] = null,
 @__startDate [datetime] = null,
 @__endDate [datetime] = null
)
AS

SET NOCOUNT ON;

if(@__pageOffset < 1 ) SET @__pageOffset = 1
if((NOT (@__recordsPerPage IS NULL)) AND (@__recordsPerPage < 1 )) SET @__recordsPerPage = 1
IF (@__recordsPerPage IS NULL)
BEGIN
SELECT
	[__AUDIT__Ancestor].[__action],
	[__AUDIT__Ancestor].[__insertdate],
	[__AUDIT__Ancestor].[ModifiedBy],
	[__AUDIT__Ancestor].[AncestorId],
	[__AUDIT__Ancestor].[Name]
FROM
	[__AUDIT__Ancestor]
WHERE
	((@__auditType IS NULL) OR ([__AUDIT__Ancestor].__action = @__auditType)) AND
	((@__startDate IS NULL) OR ([__AUDIT__Ancestor].__insertdate >= @__startDate)) AND
	((@__endDate IS NULL) OR ([__AUDIT__Ancestor].__insertdate < @__endDate))
	ORDER BY [__AUDIT__Ancestor].__insertdate DESC, [__AUDIT__Ancestor].__rowid DESC

END
ELSE
BEGIN
SELECT
	[__TempTable].[__action],
	[__TempTable].[__insertdate],
	[__TempTable].[ModifiedBy],
	[__TempTable].[AncestorId],
	[__TempTable].[Name]
FROM
	(SELECT  ROW_NUMBER() OVER (ORDER BY [__AUDIT__Ancestor].__insertdate DESC, [__AUDIT__Ancestor].__rowid DESC)
	AS __row,
	[__AUDIT__Ancestor].[__action],
	[__AUDIT__Ancestor].[__insertdate],
	[__AUDIT__Ancestor].ModifiedBy,
	[__AUDIT__Ancestor].[AncestorId],
	[__AUDIT__Ancestor].[Name]
	FROM 
		[__AUDIT__Ancestor]
	WHERE
		((@__auditType IS NULL) OR ([__AUDIT__Ancestor].__action = @__auditType)) AND
	((@__startDate IS NULL) OR ([__AUDIT__Ancestor].__insertdate >= @__startDate)) AND
	((@__endDate IS NULL) OR ([__AUDIT__Ancestor].__insertdate < @__endDate))
	)
	AS [__TempTable]
WHERE
	__row >= (((@__pageOffset-1)*@__recordsPerPage)+1) AND __row <= (@__pageOffset*@__recordsPerPage)

END
return (select COUNT(*) 
	FROM [__AUDIT__Ancestor] WHERE
		((@__auditType IS NULL) OR ([__AUDIT__Ancestor].__action = @__auditType)) AND
	((@__startDate IS NULL) OR ([__AUDIT__Ancestor].__insertdate >= @__startDate)) AND
	((@__endDate IS NULL) OR ([__AUDIT__Ancestor].__insertdate < @__endDate))
		)

GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[gen_Ancestor_Update]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
	drop procedure [dbo].[gen_Ancestor_Update]
GO

CREATE PROCEDURE [dbo].[gen_Ancestor_Update]
(
	@Name [VarChar] (50),
	@ModifiedBy [Varchar] (50) = null,
	@ModifiedDate [DateTime] = null,
	@Original_AncestorId [Int],
	@Original_Timestamp timestamp
)
AS

IF (@ModifiedDate IS NULL)
SET @ModifiedDate = getdate();

SET NOCOUNT ON;
UPDATE 
	[dbo].[Ancestor] 
SET
	[Name] = @Name,
	[ModifiedBy] = @ModifiedBy,
	[ModifiedDate] = @ModifiedDate

WHERE
	[dbo].[Ancestor].[AncestorId] = @Original_AncestorId AND
	[dbo].[Ancestor].[Timestamp] = @Original_Timestamp


if (@@RowCount = 0) return;

SELECT
	[dbo].[Ancestor].[ancestorid],
	[dbo].[Ancestor].[name],
	[dbo].[Ancestor].[CreatedBy],
	[dbo].[Ancestor].[CreatedDate],
	[dbo].[Ancestor].[ModifiedBy],
	[dbo].[Ancestor].[ModifiedDate],
	[dbo].[Ancestor].[Timestamp]
FROM 
[dbo].[Ancestor]
WHERE
	[dbo].[Ancestor].[AncestorId] = @Original_AncestorId
GO

--This SQL is generated for internal stored procedures for table [Child]

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[gen_Child_Delete]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
	drop procedure [dbo].[gen_Child_Delete]
GO

CREATE PROCEDURE [dbo].[gen_Child_Delete]
(
	@MyRoleParent_ParentId [Int] = null,--Entity Framework Required Parent Keys be passed in: Table 'Parent'
	@Original_ChildId [Int]
)
AS
SET NOCOUNT OFF;

DELETE FROM
	[dbo].[Child] 
WHERE 
	[ChildId] = @Original_ChildId;

if (@@RowCount = 0) return;


GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[gen_Child_Insert]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
	drop procedure [dbo].[gen_Child_Insert]
GO

CREATE PROCEDURE [dbo].[gen_Child_Insert]
(
	@ChildId [Int] = null,
	@FparentId [Int],
	@Name2 [VarChar] (100),
	@CreatedDate [DateTime] = null,
	@CreatedBy [Varchar] (50) = null,
	@ModifiedBy [Varchar] (50) = null

)
AS
SET NOCOUNT OFF;

if (@CreatedDate IS NULL)
SET @CreatedDate = getdate()
DECLARE @ModifiedDate [DateTime]
SET @ModifiedDate = getdate()
IF (@ChildId < 0) SET @ChildId = NULL;
if ((@ChildId IS NULL))
BEGIN
INSERT INTO [dbo].[Child]
(
	[FparentId],
	[Name2],
	[CreatedDate],
	[CreatedBy],
	[ModifiedDate],
	[ModifiedBy]
)
VALUES
(
	@FparentId,
	@Name2,
	@CreatedDate,
	@CreatedBy,
	@ModifiedDate,
	@ModifiedBy
);

if (@@RowCount = 0) return;

END
ELSE
BEGIN
SET identity_insert [dbo].[Child] on
INSERT INTO [dbo].[Child]
(
	[ChildId],
	[FparentId],
	[Name2],
	[CreatedDate],
	[CreatedBy],
	[ModifiedDate],
	[ModifiedBy]
)
VALUES
(
	@ChildId,
	@FparentId,
	@Name2,
	@CreatedDate,
	@CreatedBy,
	@ModifiedDate,
	@ModifiedBy
);

if (@@RowCount = 0) return;

SET identity_insert [dbo].[Child] off
END


SELECT 
	[dbo].[Child].[childid],
	[dbo].[Child].[fparentid],
	[dbo].[Child].[name2],
	[dbo].[Child].[CreatedBy],
	[dbo].[Child].[CreatedDate],
	[dbo].[Child].[ModifiedBy],
	[dbo].[Child].[ModifiedDate],
	[dbo].[Child].[Timestamp]

FROM
[dbo].[Child]
WHERE
	[dbo].[Child].[ChildId] = SCOPE_IDENTITY();
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[gen_Child_Update]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
	drop procedure [dbo].[gen_Child_Update]
GO

CREATE PROCEDURE [dbo].[gen_Child_Update]
(
	@FparentId [Int],
	@Name2 [VarChar] (100),
	@ModifiedBy [Varchar] (50) = null,
	@ModifiedDate [DateTime] = null,
	@Original_ChildId [Int],
	@Original_Timestamp timestamp
)
AS

IF (@ModifiedDate IS NULL)
SET @ModifiedDate = getdate();

SET NOCOUNT ON;
UPDATE 
	[dbo].[Child] 
SET
	[FparentId] = @FparentId,
	[Name2] = @Name2,
	[ModifiedBy] = @ModifiedBy,
	[ModifiedDate] = @ModifiedDate

WHERE
	[dbo].[Child].[ChildId] = @Original_ChildId AND
	[dbo].[Child].[Timestamp] = @Original_Timestamp


if (@@RowCount = 0) return;

SELECT
	[dbo].[Child].[childid],
	[dbo].[Child].[fparentid],
	[dbo].[Child].[name2],
	[dbo].[Child].[CreatedBy],
	[dbo].[Child].[CreatedDate],
	[dbo].[Child].[ModifiedBy],
	[dbo].[Child].[ModifiedDate],
	[dbo].[Child].[Timestamp]
FROM 
[dbo].[Child]
WHERE
	[dbo].[Child].[ChildId] = @Original_ChildId
GO

--This SQL is generated for internal stored procedures for table [Descendant]

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[gen_Descendant_Delete]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
	drop procedure [dbo].[gen_Descendant_Delete]
GO

CREATE PROCEDURE [dbo].[gen_Descendant_Delete]
(
	@DescendantParent_DecendantParentId [Int] = null,--Entity Framework Required Parent Keys be passed in: Table 'DescendantParent'
	@Ancestor_AncestorId [Int] = null,--Entity Framework Required Parent Keys be passed in: Table 'Ancestor'
	@Original_AncestorId [Int]
)
AS
SET NOCOUNT OFF;

DELETE FROM
	[dbo].[Descendant] 
WHERE 
	[AncestorId] = @Original_AncestorId;

if (@@RowCount = 0) return;

exec [dbo].[gen_Ancestor_Delete] @Original_AncestorId = @Original_AncestorId
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[gen_Descendant_Insert]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
	drop procedure [dbo].[gen_Descendant_Insert]
GO

CREATE PROCEDURE [dbo].[gen_Descendant_Insert]
(
	@AncestorId [Int] = null,
	@FdecendantParentId [Int] = null,
	@LastName [VarChar] (50) = 'Davis',
	@Name [VarChar] (50),
	@Price [Decimal] (20, 0),
	@CreatedDate [DateTime] = null,
	@CreatedBy [Varchar] (50) = null,
	@ModifiedBy [Varchar] (50) = null

)
AS
SET NOCOUNT OFF;

if (@CreatedDate IS NULL)
SET @CreatedDate = getdate()
DECLARE @ModifiedDate [DateTime]
SET @ModifiedDate = getdate()
IF (@AncestorId < 0) SET @AncestorId = NULL;
if ((@AncestorId IS NULL))
BEGIN
INSERT INTO [dbo].[Ancestor]
(
	[Name],
	[CreatedDate],
	[CreatedBy],
	[ModifiedDate],
	[ModifiedBy]
)
VALUES
(
	@Name,
	@CreatedDate,
	@CreatedBy,
	@ModifiedDate,
	@ModifiedBy
);

if (@@RowCount = 0) return;

END
ELSE
BEGIN
SET identity_insert [dbo].[Ancestor] on
INSERT INTO [dbo].[Ancestor]
(
	[AncestorId],
	[Name],
	[CreatedDate],
	[CreatedBy],
	[ModifiedDate],
	[ModifiedBy]
)
VALUES
(
	@AncestorId,
	@Name,
	@CreatedDate,
	@CreatedBy,
	@ModifiedDate,
	@ModifiedBy
);

if (@@RowCount = 0) return;

SET identity_insert [dbo].[Ancestor] off
END

SET @AncestorId = SCOPE_IDENTITY();
INSERT INTO [dbo].[Descendant]
(
	[AncestorId],
	[FdecendantParentId],
	[LastName],
	[Price],
	[CreatedDate],
	[CreatedBy],
	[ModifiedDate],
	[ModifiedBy]
)
VALUES
(
	@AncestorId,
	@FdecendantParentId,
	@LastName,
	@Price,
	@CreatedDate,
	@CreatedBy,
	@ModifiedDate,
	@ModifiedBy
);

if (@@RowCount = 0) return;



SELECT 
	[dbo].[Descendant].[ancestorid],
	[dbo].[Descendant].[fdecendantparentid],
	[dbo].[Descendant].[lastname],
	[dbo].[Ancestor].[name],
	[dbo].[Descendant].[price],
	[dbo].[Descendant].[CreatedBy],
	[dbo].[Descendant].[CreatedDate],
	[dbo].[Descendant].[ModifiedBy],
	[dbo].[Descendant].[ModifiedDate],
	[dbo].[Ancestor].[Timestamp]

FROM
[dbo].[Descendant] INNER JOIN [dbo].[Ancestor] ON  [dbo].[Descendant].[AncestorId] = [dbo].[Ancestor].[AncestorId]
WHERE
	[dbo].[Descendant].[AncestorId] = @AncestorId;
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[gen_Descendant__AUDIT_SELECT]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
	drop procedure [dbo].[gen_Descendant__AUDIT_SELECT]
GO

CREATE PROCEDURE [dbo].[gen_Descendant__AUDIT_SELECT]
(
 @AncestorId [Int],
 @__auditType [int] = null,
 @__pageOffset [int] = 1,
 @__recordsPerPage [int] = null,
 @__startDate [datetime] = null,
 @__endDate [datetime] = null
)
AS

SET NOCOUNT ON;

if(@__pageOffset < 1 ) SET @__pageOffset = 1
if((NOT (@__recordsPerPage IS NULL)) AND (@__recordsPerPage < 1 )) SET @__recordsPerPage = 1
IF (@__recordsPerPage IS NULL)
BEGIN
SELECT
	[__AUDIT__Descendant].[__action],
	[__AUDIT__Descendant].[__insertdate],
	[__AUDIT__Descendant].[ModifiedBy],
	[__AUDIT__Descendant].[AncestorId],
	[__AUDIT__Descendant].[FdecendantParentId],
	[__AUDIT__Descendant].[LastName],
	[__AUDIT__Descendant].[Price]
FROM
	[__AUDIT__Descendant]
WHERE
	((@__auditType IS NULL) OR ([__AUDIT__Descendant].__action = @__auditType)) AND
	((@__startDate IS NULL) OR ([__AUDIT__Descendant].__insertdate >= @__startDate)) AND
	((@__endDate IS NULL) OR ([__AUDIT__Descendant].__insertdate < @__endDate)) AND
	(@AncestorId = [__AUDIT__Descendant].AncestorId)
	ORDER BY [__AUDIT__Descendant].__insertdate DESC, [__AUDIT__Descendant].__rowid DESC

END
ELSE
BEGIN
SELECT
	[__TempTable].[__action],
	[__TempTable].[__insertdate],
	[__TempTable].[ModifiedBy],
	[__TempTable].[AncestorId],
	[__TempTable].[FdecendantParentId],
	[__TempTable].[LastName],
	[__TempTable].[Price]
FROM
	(SELECT  ROW_NUMBER() OVER (ORDER BY [__AUDIT__Descendant].__insertdate DESC, [__AUDIT__Descendant].__rowid DESC)
	AS __row,
	[__AUDIT__Descendant].[__action],
	[__AUDIT__Descendant].[__insertdate],
	[__AUDIT__Descendant].ModifiedBy,
	[__AUDIT__Descendant].[AncestorId],
	[__AUDIT__Descendant].[FdecendantParentId],
	[__AUDIT__Descendant].[LastName],
	[__AUDIT__Descendant].[Price]
	FROM 
		[__AUDIT__Descendant]
	WHERE
		((@__auditType IS NULL) OR ([__AUDIT__Descendant].__action = @__auditType)) AND
	((@__startDate IS NULL) OR ([__AUDIT__Descendant].__insertdate >= @__startDate)) AND
	((@__endDate IS NULL) OR ([__AUDIT__Descendant].__insertdate < @__endDate)) AND
		(@AncestorId = [__AUDIT__Descendant].AncestorId)
	)
	AS [__TempTable]
WHERE
	__row >= (((@__pageOffset-1)*@__recordsPerPage)+1) AND __row <= (@__pageOffset*@__recordsPerPage)

END
return (select COUNT(*) 
	FROM [__AUDIT__Descendant] WHERE
		((@__auditType IS NULL) OR ([__AUDIT__Descendant].__action = @__auditType)) AND
	((@__startDate IS NULL) OR ([__AUDIT__Descendant].__insertdate >= @__startDate)) AND
	((@__endDate IS NULL) OR ([__AUDIT__Descendant].__insertdate < @__endDate)) AND
		(@AncestorId = [__AUDIT__Descendant].AncestorId)
		)

GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[gen_Descendant__AUDIT_SELECT_ALL]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
	drop procedure [dbo].[gen_Descendant__AUDIT_SELECT_ALL]
GO

CREATE PROCEDURE [dbo].[gen_Descendant__AUDIT_SELECT_ALL]
(
 @__auditType [int] = null,
 @__pageOffset [int] = 1,
 @__recordsPerPage [int] = null,
 @__startDate [datetime] = null,
 @__endDate [datetime] = null
)
AS

SET NOCOUNT ON;

if(@__pageOffset < 1 ) SET @__pageOffset = 1
if((NOT (@__recordsPerPage IS NULL)) AND (@__recordsPerPage < 1 )) SET @__recordsPerPage = 1
IF (@__recordsPerPage IS NULL)
BEGIN
SELECT
	[__AUDIT__Descendant].[__action],
	[__AUDIT__Descendant].[__insertdate],
	[__AUDIT__Descendant].[ModifiedBy],
	[__AUDIT__Descendant].[AncestorId],
	[__AUDIT__Descendant].[FdecendantParentId],
	[__AUDIT__Descendant].[LastName],
	[__AUDIT__Descendant].[Price]
FROM
	[__AUDIT__Descendant]
WHERE
	((@__auditType IS NULL) OR ([__AUDIT__Descendant].__action = @__auditType)) AND
	((@__startDate IS NULL) OR ([__AUDIT__Descendant].__insertdate >= @__startDate)) AND
	((@__endDate IS NULL) OR ([__AUDIT__Descendant].__insertdate < @__endDate))
	ORDER BY [__AUDIT__Descendant].__insertdate DESC, [__AUDIT__Descendant].__rowid DESC

END
ELSE
BEGIN
SELECT
	[__TempTable].[__action],
	[__TempTable].[__insertdate],
	[__TempTable].[ModifiedBy],
	[__TempTable].[AncestorId],
	[__TempTable].[FdecendantParentId],
	[__TempTable].[LastName],
	[__TempTable].[Price]
FROM
	(SELECT  ROW_NUMBER() OVER (ORDER BY [__AUDIT__Descendant].__insertdate DESC, [__AUDIT__Descendant].__rowid DESC)
	AS __row,
	[__AUDIT__Descendant].[__action],
	[__AUDIT__Descendant].[__insertdate],
	[__AUDIT__Descendant].ModifiedBy,
	[__AUDIT__Descendant].[AncestorId],
	[__AUDIT__Descendant].[FdecendantParentId],
	[__AUDIT__Descendant].[LastName],
	[__AUDIT__Descendant].[Price]
	FROM 
		[__AUDIT__Descendant]
	WHERE
		((@__auditType IS NULL) OR ([__AUDIT__Descendant].__action = @__auditType)) AND
	((@__startDate IS NULL) OR ([__AUDIT__Descendant].__insertdate >= @__startDate)) AND
	((@__endDate IS NULL) OR ([__AUDIT__Descendant].__insertdate < @__endDate))
	)
	AS [__TempTable]
WHERE
	__row >= (((@__pageOffset-1)*@__recordsPerPage)+1) AND __row <= (@__pageOffset*@__recordsPerPage)

END
return (select COUNT(*) 
	FROM [__AUDIT__Descendant] WHERE
		((@__auditType IS NULL) OR ([__AUDIT__Descendant].__action = @__auditType)) AND
	((@__startDate IS NULL) OR ([__AUDIT__Descendant].__insertdate >= @__startDate)) AND
	((@__endDate IS NULL) OR ([__AUDIT__Descendant].__insertdate < @__endDate))
		)

GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[gen_Descendant_Update]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
	drop procedure [dbo].[gen_Descendant_Update]
GO

CREATE PROCEDURE [dbo].[gen_Descendant_Update]
(
	@FdecendantParentId [Int] = null,
	@LastName [VarChar] (50) = 'Davis',
	@Name [VarChar] (50),
	@Price [Decimal] (20, 0),
	@ModifiedBy [Varchar] (50) = null,
	@ModifiedDate [DateTime] = null,
	@Original_AncestorId [Int],
	@Original_Timestamp timestamp
)
AS

IF (@ModifiedDate IS NULL)
SET @ModifiedDate = getdate();

SET NOCOUNT ON;
UPDATE 
	[dbo].[Ancestor] 
SET
	[Name] = @Name,
	[ModifiedBy] = @ModifiedBy,
	[ModifiedDate] = @ModifiedDate

WHERE
	[dbo].[Ancestor].[AncestorId] = @Original_AncestorId AND
	[dbo].[Ancestor].[Timestamp] = @Original_Timestamp


if (@@RowCount = 0) return;

UPDATE 
	[dbo].[Descendant] 
SET
	[FdecendantParentId] = @FdecendantParentId,
	[LastName] = @LastName,
	[Price] = @Price,
	[ModifiedBy] = @ModifiedBy,
	[ModifiedDate] = @ModifiedDate

WHERE
	[dbo].[Descendant].[AncestorId] = @Original_AncestorId


if (@@RowCount = 0) return;

SELECT
	[dbo].[Descendant].[ancestorid],
	[dbo].[Descendant].[fdecendantparentid],
	[dbo].[Descendant].[lastname],
	[dbo].[Ancestor].[name],
	[dbo].[Descendant].[price],
	[dbo].[Descendant].[CreatedBy],
	[dbo].[Descendant].[CreatedDate],
	[dbo].[Descendant].[ModifiedBy],
	[dbo].[Descendant].[ModifiedDate],
	[dbo].[Ancestor].[Timestamp]
FROM 
[dbo].[Descendant] INNER JOIN [dbo].[Ancestor] ON  [dbo].[Descendant].[AncestorId] = [dbo].[Ancestor].[AncestorId]
WHERE
	[dbo].[Descendant].[AncestorId] = @Original_AncestorId
GO

--This SQL is generated for internal stored procedures for table [DescendantParent]

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[gen_DescendantParent_Delete]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
	drop procedure [dbo].[gen_DescendantParent_Delete]
GO

CREATE PROCEDURE [dbo].[gen_DescendantParent_Delete]
(
	@Original_DecendantParentId [Int]
)
AS
SET NOCOUNT OFF;

DELETE FROM
	[dbo].[DescendantParent] 
WHERE 
	[DecendantParentId] = @Original_DecendantParentId;

if (@@RowCount = 0) return;


GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[gen_DescendantParent_Insert]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
	drop procedure [dbo].[gen_DescendantParent_Insert]
GO

CREATE PROCEDURE [dbo].[gen_DescendantParent_Insert]
(
	@DecendantParentId [Int] = null,
	@Qqq [VarChar] (50) = null,
	@CreatedDate [DateTime] = null,
	@CreatedBy [Varchar] (50) = null,
	@ModifiedBy [Varchar] (50) = null

)
AS
SET NOCOUNT OFF;

if (@CreatedDate IS NULL)
SET @CreatedDate = getdate()
DECLARE @ModifiedDate [DateTime]
SET @ModifiedDate = getdate()
IF (@DecendantParentId < 0) SET @DecendantParentId = NULL;
if ((@DecendantParentId IS NULL))
BEGIN
INSERT INTO [dbo].[DescendantParent]
(
	[Qqq],
	[CreatedDate],
	[CreatedBy],
	[ModifiedDate],
	[ModifiedBy]
)
VALUES
(
	@Qqq,
	@CreatedDate,
	@CreatedBy,
	@ModifiedDate,
	@ModifiedBy
);

if (@@RowCount = 0) return;

END
ELSE
BEGIN
SET identity_insert [dbo].[DescendantParent] on
INSERT INTO [dbo].[DescendantParent]
(
	[DecendantParentId],
	[Qqq],
	[CreatedDate],
	[CreatedBy],
	[ModifiedDate],
	[ModifiedBy]
)
VALUES
(
	@DecendantParentId,
	@Qqq,
	@CreatedDate,
	@CreatedBy,
	@ModifiedDate,
	@ModifiedBy
);

if (@@RowCount = 0) return;

SET identity_insert [dbo].[DescendantParent] off
END


SELECT 
	[dbo].[DescendantParent].[decendantparentid],
	[dbo].[DescendantParent].[qqq],
	[dbo].[DescendantParent].[CreatedBy],
	[dbo].[DescendantParent].[CreatedDate],
	[dbo].[DescendantParent].[ModifiedBy],
	[dbo].[DescendantParent].[ModifiedDate],
	[dbo].[DescendantParent].[Timestamp]

FROM
[dbo].[DescendantParent]
WHERE
	[dbo].[DescendantParent].[DecendantParentId] = SCOPE_IDENTITY();
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[gen_DescendantParent_Update]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
	drop procedure [dbo].[gen_DescendantParent_Update]
GO

CREATE PROCEDURE [dbo].[gen_DescendantParent_Update]
(
	@Qqq [VarChar] (50) = null,
	@ModifiedBy [Varchar] (50) = null,
	@ModifiedDate [DateTime] = null,
	@Original_DecendantParentId [Int],
	@Original_Timestamp timestamp
)
AS

IF (@ModifiedDate IS NULL)
SET @ModifiedDate = getdate();

SET NOCOUNT ON;
UPDATE 
	[dbo].[DescendantParent] 
SET
	[Qqq] = @Qqq,
	[ModifiedBy] = @ModifiedBy,
	[ModifiedDate] = @ModifiedDate

WHERE
	[dbo].[DescendantParent].[DecendantParentId] = @Original_DecendantParentId AND
	[dbo].[DescendantParent].[Timestamp] = @Original_Timestamp


if (@@RowCount = 0) return;

SELECT
	[dbo].[DescendantParent].[decendantparentid],
	[dbo].[DescendantParent].[qqq],
	[dbo].[DescendantParent].[CreatedBy],
	[dbo].[DescendantParent].[CreatedDate],
	[dbo].[DescendantParent].[ModifiedBy],
	[dbo].[DescendantParent].[ModifiedDate],
	[dbo].[DescendantParent].[Timestamp]
FROM 
[dbo].[DescendantParent]
WHERE
	[dbo].[DescendantParent].[DecendantParentId] = @Original_DecendantParentId
GO

--This SQL is generated for internal stored procedures for table [DoubleKeyIdentity]

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[gen_DoubleKeyIdentity_Delete]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
	drop procedure [dbo].[gen_DoubleKeyIdentity_Delete]
GO

CREATE PROCEDURE [dbo].[gen_DoubleKeyIdentity_Delete]
(
	@Original_Key [Int],
	@Original_Pk1 [VarChar] (50)
)
AS
SET NOCOUNT OFF;

DELETE FROM
	[dbo].[DoubleKeyIdentity] 
WHERE 
	[Key] = @Original_Key AND
	[Pk1] = @Original_Pk1;

if (@@RowCount = 0) return;


GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[gen_DoubleKeyIdentity_Insert]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
	drop procedure [dbo].[gen_DoubleKeyIdentity_Insert]
GO

CREATE PROCEDURE [dbo].[gen_DoubleKeyIdentity_Insert]
(
	@Key [Int] = null,
	@Name [VarChar] (50) = null,
	@Pk1 [VarChar] (50),
	@CreatedDate [DateTime] = null,
	@CreatedBy [Varchar] (50) = null,
	@ModifiedBy [Varchar] (50) = null

)
AS
SET NOCOUNT OFF;

if (@CreatedDate IS NULL)
SET @CreatedDate = getdate()
DECLARE @ModifiedDate [DateTime]
SET @ModifiedDate = getdate()
IF (@Key < 0) SET @Key = NULL;
if ((@Key IS NULL))
BEGIN
INSERT INTO [dbo].[DoubleKeyIdentity]
(
	[Name],
	[Pk1],
	[CreatedDate],
	[CreatedBy],
	[ModifiedDate],
	[ModifiedBy]
)
VALUES
(
	@Name,
	@Pk1,
	@CreatedDate,
	@CreatedBy,
	@ModifiedDate,
	@ModifiedBy
);

if (@@RowCount = 0) return;

END
ELSE
BEGIN
SET identity_insert [dbo].[DoubleKeyIdentity] on
INSERT INTO [dbo].[DoubleKeyIdentity]
(
	[Key],
	[Name],
	[Pk1],
	[CreatedDate],
	[CreatedBy],
	[ModifiedDate],
	[ModifiedBy]
)
VALUES
(
	@Key,
	@Name,
	@Pk1,
	@CreatedDate,
	@CreatedBy,
	@ModifiedDate,
	@ModifiedBy
);

if (@@RowCount = 0) return;

SET identity_insert [dbo].[DoubleKeyIdentity] off
END


SELECT 
	[dbo].[DoubleKeyIdentity].[key],
	[dbo].[DoubleKeyIdentity].[name],
	[dbo].[DoubleKeyIdentity].[pk1],
	[dbo].[DoubleKeyIdentity].[CreatedBy],
	[dbo].[DoubleKeyIdentity].[CreatedDate],
	[dbo].[DoubleKeyIdentity].[ModifiedBy],
	[dbo].[DoubleKeyIdentity].[ModifiedDate],
	[dbo].[DoubleKeyIdentity].[Timestamp]

FROM
[dbo].[DoubleKeyIdentity]
WHERE
	[dbo].[DoubleKeyIdentity].[Key] = SCOPE_IDENTITY() AND
	[dbo].[DoubleKeyIdentity].[Pk1] = @Pk1;
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[gen_DoubleKeyIdentity_Update]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
	drop procedure [dbo].[gen_DoubleKeyIdentity_Update]
GO

CREATE PROCEDURE [dbo].[gen_DoubleKeyIdentity_Update]
(
	@Name [VarChar] (50) = null,
	@ModifiedBy [Varchar] (50) = null,
	@ModifiedDate [DateTime] = null,
	@Original_Key [Int],
	@Original_Pk1 [VarChar] (50),
	@Original_Timestamp timestamp
)
AS

IF (@ModifiedDate IS NULL)
SET @ModifiedDate = getdate();

SET NOCOUNT ON;
UPDATE 
	[dbo].[DoubleKeyIdentity] 
SET
	[Name] = @Name,
	[ModifiedBy] = @ModifiedBy,
	[ModifiedDate] = @ModifiedDate

WHERE
	[dbo].[DoubleKeyIdentity].[Key] = @Original_Key AND
	[dbo].[DoubleKeyIdentity].[Pk1] = @Original_Pk1 AND
	[dbo].[DoubleKeyIdentity].[Timestamp] = @Original_Timestamp


if (@@RowCount = 0) return;

SELECT
	[dbo].[DoubleKeyIdentity].[key],
	[dbo].[DoubleKeyIdentity].[name],
	[dbo].[DoubleKeyIdentity].[pk1],
	[dbo].[DoubleKeyIdentity].[CreatedBy],
	[dbo].[DoubleKeyIdentity].[CreatedDate],
	[dbo].[DoubleKeyIdentity].[ModifiedBy],
	[dbo].[DoubleKeyIdentity].[ModifiedDate],
	[dbo].[DoubleKeyIdentity].[Timestamp]
FROM 
[dbo].[DoubleKeyIdentity]
WHERE
	[dbo].[DoubleKeyIdentity].[Key] = @Original_Key AND
	[dbo].[DoubleKeyIdentity].[Pk1] = @Original_Pk1
GO

--This SQL is generated for internal stored procedures for table [DoubleLinkOneOneChild]

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[gen_DoubleLinkOneOneChild_Delete]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
	drop procedure [dbo].[gen_DoubleLinkOneOneChild_Delete]
GO

CREATE PROCEDURE [dbo].[gen_DoubleLinkOneOneChild_Delete]
(
	@DoubleLinkOneOneParent_DoubleLinkParentKey1 [Int] = null,--Entity Framework Required Parent Keys be passed in: Table 'DoubleLinkOneOneParent'
	@DoubleLinkOneOneParent_DoubleLinkParentKey2 [Int] = null,--Entity Framework Required Parent Keys be passed in: Table 'DoubleLinkOneOneParent'
	@Original_DoubleLinkChildKey1 [Int],
	@Original_DoubleLinkChildKey2 [Int]
)
AS
SET NOCOUNT OFF;

DELETE FROM
	[dbo].[DoubleLinkOneOneChild] 
WHERE 
	[DoubleLinkChildKey1] = @Original_DoubleLinkChildKey1 AND
	[DoubleLinkChildKey2] = @Original_DoubleLinkChildKey2;

if (@@RowCount = 0) return;


GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[gen_DoubleLinkOneOneChild_Insert]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
	drop procedure [dbo].[gen_DoubleLinkOneOneChild_Insert]
GO

CREATE PROCEDURE [dbo].[gen_DoubleLinkOneOneChild_Insert]
(
	@DoubleLinkChildKey1 [Int],
	@DoubleLinkChildKey2 [Int],
	@DoubleLinkParentKey1 [Int],
	@DoubleLinkParentKey2 [Int],
	@Qqq [VarChar] (50) = null,
	@CreatedDate [DateTime] = null,
	@CreatedBy [Varchar] (50) = null,
	@ModifiedBy [Varchar] (50) = null

)
AS
SET NOCOUNT OFF;

if (@CreatedDate IS NULL)
SET @CreatedDate = getdate()
DECLARE @ModifiedDate [DateTime]
SET @ModifiedDate = getdate()
INSERT INTO [dbo].[DoubleLinkOneOneChild]
(
	[DoubleLinkChildKey1],
	[DoubleLinkChildKey2],
	[DoubleLinkParentKey1],
	[DoubleLinkParentKey2],
	[Qqq],
	[CreatedDate],
	[CreatedBy],
	[ModifiedDate],
	[ModifiedBy]
)
VALUES
(
	@DoubleLinkChildKey1,
	@DoubleLinkChildKey2,
	@DoubleLinkParentKey1,
	@DoubleLinkParentKey2,
	@Qqq,
	@CreatedDate,
	@CreatedBy,
	@ModifiedDate,
	@ModifiedBy
);

if (@@RowCount = 0) return;



SELECT 
	[dbo].[DoubleLinkOneOneChild].[doublelinkchildkey1],
	[dbo].[DoubleLinkOneOneChild].[doublelinkchildkey2],
	[dbo].[DoubleLinkOneOneChild].[doublelinkparentkey1],
	[dbo].[DoubleLinkOneOneChild].[doublelinkparentkey2],
	[dbo].[DoubleLinkOneOneChild].[qqq],
	[dbo].[DoubleLinkOneOneChild].[CreatedBy],
	[dbo].[DoubleLinkOneOneChild].[CreatedDate],
	[dbo].[DoubleLinkOneOneChild].[ModifiedBy],
	[dbo].[DoubleLinkOneOneChild].[ModifiedDate],
	[dbo].[DoubleLinkOneOneChild].[Timestamp]

FROM
[dbo].[DoubleLinkOneOneChild]
WHERE
	[dbo].[DoubleLinkOneOneChild].[DoubleLinkChildKey1] = @DoubleLinkChildKey1 AND
	[dbo].[DoubleLinkOneOneChild].[DoubleLinkChildKey2] = @DoubleLinkChildKey2;
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[gen_DoubleLinkOneOneChild_Update]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
	drop procedure [dbo].[gen_DoubleLinkOneOneChild_Update]
GO

CREATE PROCEDURE [dbo].[gen_DoubleLinkOneOneChild_Update]
(
	@DoubleLinkParentKey1 [Int],
	@DoubleLinkParentKey2 [Int],
	@Qqq [VarChar] (50) = null,
	@ModifiedBy [Varchar] (50) = null,
	@ModifiedDate [DateTime] = null,
	@Original_DoubleLinkChildKey1 [Int],
	@Original_DoubleLinkChildKey2 [Int],
	@Original_Timestamp timestamp
)
AS

IF (@ModifiedDate IS NULL)
SET @ModifiedDate = getdate();

SET NOCOUNT ON;
UPDATE 
	[dbo].[DoubleLinkOneOneChild] 
SET
	[DoubleLinkParentKey1] = @DoubleLinkParentKey1,
	[DoubleLinkParentKey2] = @DoubleLinkParentKey2,
	[Qqq] = @Qqq,
	[ModifiedBy] = @ModifiedBy,
	[ModifiedDate] = @ModifiedDate

WHERE
	[dbo].[DoubleLinkOneOneChild].[DoubleLinkChildKey1] = @Original_DoubleLinkChildKey1 AND
	[dbo].[DoubleLinkOneOneChild].[DoubleLinkChildKey2] = @Original_DoubleLinkChildKey2 AND
	[dbo].[DoubleLinkOneOneChild].[Timestamp] = @Original_Timestamp


if (@@RowCount = 0) return;

SELECT
	[dbo].[DoubleLinkOneOneChild].[doublelinkchildkey1],
	[dbo].[DoubleLinkOneOneChild].[doublelinkchildkey2],
	[dbo].[DoubleLinkOneOneChild].[doublelinkparentkey1],
	[dbo].[DoubleLinkOneOneChild].[doublelinkparentkey2],
	[dbo].[DoubleLinkOneOneChild].[qqq],
	[dbo].[DoubleLinkOneOneChild].[CreatedBy],
	[dbo].[DoubleLinkOneOneChild].[CreatedDate],
	[dbo].[DoubleLinkOneOneChild].[ModifiedBy],
	[dbo].[DoubleLinkOneOneChild].[ModifiedDate],
	[dbo].[DoubleLinkOneOneChild].[Timestamp]
FROM 
[dbo].[DoubleLinkOneOneChild]
WHERE
	[dbo].[DoubleLinkOneOneChild].[DoubleLinkChildKey1] = @Original_DoubleLinkChildKey1 AND
	[dbo].[DoubleLinkOneOneChild].[DoubleLinkChildKey2] = @Original_DoubleLinkChildKey2
GO

--This SQL is generated for internal stored procedures for table [DoubleLinkOneOneParent]

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[gen_DoubleLinkOneOneParent_Delete]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
	drop procedure [dbo].[gen_DoubleLinkOneOneParent_Delete]
GO

CREATE PROCEDURE [dbo].[gen_DoubleLinkOneOneParent_Delete]
(
	@Original_DoubleLinkParentKey1 [Int],
	@Original_DoubleLinkParentKey2 [Int]
)
AS
SET NOCOUNT OFF;

DELETE FROM
	[dbo].[DoubleLinkOneOneParent] 
WHERE 
	[DoubleLinkParentKey1] = @Original_DoubleLinkParentKey1 AND
	[DoubleLinkParentKey2] = @Original_DoubleLinkParentKey2;

if (@@RowCount = 0) return;


GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[gen_DoubleLinkOneOneParent_Insert]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
	drop procedure [dbo].[gen_DoubleLinkOneOneParent_Insert]
GO

CREATE PROCEDURE [dbo].[gen_DoubleLinkOneOneParent_Insert]
(
	@DoubleLinkParentKey1 [Int],
	@DoubleLinkParentKey2 [Int],
	@Name [VarChar] (50) = null,
	@CreatedDate [DateTime] = null,
	@CreatedBy [Varchar] (50) = null,
	@ModifiedBy [Varchar] (50) = null

)
AS
SET NOCOUNT OFF;

if (@CreatedDate IS NULL)
SET @CreatedDate = getdate()
DECLARE @ModifiedDate [DateTime]
SET @ModifiedDate = getdate()
INSERT INTO [dbo].[DoubleLinkOneOneParent]
(
	[DoubleLinkParentKey1],
	[DoubleLinkParentKey2],
	[Name],
	[CreatedDate],
	[CreatedBy],
	[ModifiedDate],
	[ModifiedBy]
)
VALUES
(
	@DoubleLinkParentKey1,
	@DoubleLinkParentKey2,
	@Name,
	@CreatedDate,
	@CreatedBy,
	@ModifiedDate,
	@ModifiedBy
);

if (@@RowCount = 0) return;



SELECT 
	[dbo].[DoubleLinkOneOneParent].[doublelinkparentkey1],
	[dbo].[DoubleLinkOneOneParent].[doublelinkparentkey2],
	[dbo].[DoubleLinkOneOneParent].[name],
	[dbo].[DoubleLinkOneOneParent].[CreatedBy],
	[dbo].[DoubleLinkOneOneParent].[CreatedDate],
	[dbo].[DoubleLinkOneOneParent].[ModifiedBy],
	[dbo].[DoubleLinkOneOneParent].[ModifiedDate],
	[dbo].[DoubleLinkOneOneParent].[Timestamp]

FROM
[dbo].[DoubleLinkOneOneParent]
WHERE
	[dbo].[DoubleLinkOneOneParent].[DoubleLinkParentKey1] = @DoubleLinkParentKey1 AND
	[dbo].[DoubleLinkOneOneParent].[DoubleLinkParentKey2] = @DoubleLinkParentKey2;
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[gen_DoubleLinkOneOneParent_Update]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
	drop procedure [dbo].[gen_DoubleLinkOneOneParent_Update]
GO

CREATE PROCEDURE [dbo].[gen_DoubleLinkOneOneParent_Update]
(
	@Name [VarChar] (50) = null,
	@ModifiedBy [Varchar] (50) = null,
	@ModifiedDate [DateTime] = null,
	@Original_DoubleLinkParentKey1 [Int],
	@Original_DoubleLinkParentKey2 [Int],
	@Original_Timestamp timestamp
)
AS

IF (@ModifiedDate IS NULL)
SET @ModifiedDate = getdate();

SET NOCOUNT ON;
UPDATE 
	[dbo].[DoubleLinkOneOneParent] 
SET
	[Name] = @Name,
	[ModifiedBy] = @ModifiedBy,
	[ModifiedDate] = @ModifiedDate

WHERE
	[dbo].[DoubleLinkOneOneParent].[DoubleLinkParentKey1] = @Original_DoubleLinkParentKey1 AND
	[dbo].[DoubleLinkOneOneParent].[DoubleLinkParentKey2] = @Original_DoubleLinkParentKey2 AND
	[dbo].[DoubleLinkOneOneParent].[Timestamp] = @Original_Timestamp


if (@@RowCount = 0) return;

SELECT
	[dbo].[DoubleLinkOneOneParent].[doublelinkparentkey1],
	[dbo].[DoubleLinkOneOneParent].[doublelinkparentkey2],
	[dbo].[DoubleLinkOneOneParent].[name],
	[dbo].[DoubleLinkOneOneParent].[CreatedBy],
	[dbo].[DoubleLinkOneOneParent].[CreatedDate],
	[dbo].[DoubleLinkOneOneParent].[ModifiedBy],
	[dbo].[DoubleLinkOneOneParent].[ModifiedDate],
	[dbo].[DoubleLinkOneOneParent].[Timestamp]
FROM 
[dbo].[DoubleLinkOneOneParent]
WHERE
	[dbo].[DoubleLinkOneOneParent].[DoubleLinkParentKey1] = @Original_DoubleLinkParentKey1 AND
	[dbo].[DoubleLinkOneOneParent].[DoubleLinkParentKey2] = @Original_DoubleLinkParentKey2
GO

--This SQL is generated for internal stored procedures for table [GuidLinkChild]

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[gen_GuidLinkChild_Delete]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
	drop procedure [dbo].[gen_GuidLinkChild_Delete]
GO

CREATE PROCEDURE [dbo].[gen_GuidLinkChild_Delete]
(
	@GuidLinkParent_Id [UniqueIdentifier] = null,--Entity Framework Required Parent Keys be passed in: Table 'GuidLinkParent'
	@Original_ChildId [Int]
)
AS
SET NOCOUNT OFF;

DELETE FROM
	[dbo].[GuidLinkChild] 
WHERE 
	[ChildId] = @Original_ChildId;

if (@@RowCount = 0) return;


GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[gen_GuidLinkChild_Insert]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
	drop procedure [dbo].[gen_GuidLinkChild_Insert]
GO

CREATE PROCEDURE [dbo].[gen_GuidLinkChild_Insert]
(
	@ChildId [Int] = null,
	@ParentId [UniqueIdentifier],
	@CreatedDate [DateTime] = null,
	@CreatedBy [Varchar] (50) = null,
	@ModifiedBy [Varchar] (50) = null

)
AS
SET NOCOUNT OFF;

if (@CreatedDate IS NULL)
SET @CreatedDate = getdate()
DECLARE @ModifiedDate [DateTime]
SET @ModifiedDate = getdate()
IF (@ChildId < 0) SET @ChildId = NULL;
if ((@ChildId IS NULL))
BEGIN
INSERT INTO [dbo].[GuidLinkChild]
(
	[ParentId],
	[CreatedDate],
	[CreatedBy],
	[ModifiedDate],
	[ModifiedBy]
)
VALUES
(
	@ParentId,
	@CreatedDate,
	@CreatedBy,
	@ModifiedDate,
	@ModifiedBy
);

if (@@RowCount = 0) return;

END
ELSE
BEGIN
SET identity_insert [dbo].[GuidLinkChild] on
INSERT INTO [dbo].[GuidLinkChild]
(
	[ChildId],
	[ParentId],
	[CreatedDate],
	[CreatedBy],
	[ModifiedDate],
	[ModifiedBy]
)
VALUES
(
	@ChildId,
	@ParentId,
	@CreatedDate,
	@CreatedBy,
	@ModifiedDate,
	@ModifiedBy
);

if (@@RowCount = 0) return;

SET identity_insert [dbo].[GuidLinkChild] off
END


SELECT 
	[dbo].[GuidLinkChild].[childid],
	[dbo].[GuidLinkChild].[parentid],
	[dbo].[GuidLinkChild].[CreatedBy],
	[dbo].[GuidLinkChild].[CreatedDate],
	[dbo].[GuidLinkChild].[ModifiedBy],
	[dbo].[GuidLinkChild].[ModifiedDate],
	[dbo].[GuidLinkChild].[Timestamp]

FROM
[dbo].[GuidLinkChild]
WHERE
	[dbo].[GuidLinkChild].[ChildId] = SCOPE_IDENTITY();
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[gen_GuidLinkChild_Update]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
	drop procedure [dbo].[gen_GuidLinkChild_Update]
GO

CREATE PROCEDURE [dbo].[gen_GuidLinkChild_Update]
(
	@ParentId [UniqueIdentifier],
	@ModifiedBy [Varchar] (50) = null,
	@ModifiedDate [DateTime] = null,
	@Original_ChildId [Int],
	@Original_Timestamp timestamp
)
AS

IF (@ModifiedDate IS NULL)
SET @ModifiedDate = getdate();

SET NOCOUNT ON;
UPDATE 
	[dbo].[GuidLinkChild] 
SET
	[ParentId] = @ParentId,
	[ModifiedBy] = @ModifiedBy,
	[ModifiedDate] = @ModifiedDate

WHERE
	[dbo].[GuidLinkChild].[ChildId] = @Original_ChildId AND
	[dbo].[GuidLinkChild].[Timestamp] = @Original_Timestamp


if (@@RowCount = 0) return;

SELECT
	[dbo].[GuidLinkChild].[childid],
	[dbo].[GuidLinkChild].[parentid],
	[dbo].[GuidLinkChild].[CreatedBy],
	[dbo].[GuidLinkChild].[CreatedDate],
	[dbo].[GuidLinkChild].[ModifiedBy],
	[dbo].[GuidLinkChild].[ModifiedDate],
	[dbo].[GuidLinkChild].[Timestamp]
FROM 
[dbo].[GuidLinkChild]
WHERE
	[dbo].[GuidLinkChild].[ChildId] = @Original_ChildId
GO

--This SQL is generated for internal stored procedures for table [GuidLinkParent]

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[gen_GuidLinkParent_Delete]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
	drop procedure [dbo].[gen_GuidLinkParent_Delete]
GO

CREATE PROCEDURE [dbo].[gen_GuidLinkParent_Delete]
(
	@Original_Id [UniqueIdentifier]
)
AS
SET NOCOUNT OFF;

DELETE FROM
	[dbo].[GuidLinkParent] 
WHERE 
	[Id] = @Original_Id;

if (@@RowCount = 0) return;


GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[gen_GuidLinkParent_Insert]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
	drop procedure [dbo].[gen_GuidLinkParent_Insert]
GO

CREATE PROCEDURE [dbo].[gen_GuidLinkParent_Insert]
(
	@Id [UniqueIdentifier],
	@Name [VarChar] (50),
	@CreatedDate [DateTime] = null,
	@CreatedBy [Varchar] (50) = null,
	@ModifiedBy [Varchar] (50) = null

)
AS
SET NOCOUNT OFF;

if (@CreatedDate IS NULL)
SET @CreatedDate = getdate()
DECLARE @ModifiedDate [DateTime]
SET @ModifiedDate = getdate()
INSERT INTO [dbo].[GuidLinkParent]
(
	[Id],
	[Name],
	[CreatedDate],
	[CreatedBy],
	[ModifiedDate],
	[ModifiedBy]
)
VALUES
(
	@Id,
	@Name,
	@CreatedDate,
	@CreatedBy,
	@ModifiedDate,
	@ModifiedBy
);

if (@@RowCount = 0) return;



SELECT 
	[dbo].[GuidLinkParent].[id],
	[dbo].[GuidLinkParent].[name],
	[dbo].[GuidLinkParent].[CreatedBy],
	[dbo].[GuidLinkParent].[CreatedDate],
	[dbo].[GuidLinkParent].[ModifiedBy],
	[dbo].[GuidLinkParent].[ModifiedDate],
	[dbo].[GuidLinkParent].[Timestamp]

FROM
[dbo].[GuidLinkParent]
WHERE
	[dbo].[GuidLinkParent].[Id] = @Id;
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[gen_GuidLinkParent_Update]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
	drop procedure [dbo].[gen_GuidLinkParent_Update]
GO

CREATE PROCEDURE [dbo].[gen_GuidLinkParent_Update]
(
	@Name [VarChar] (50),
	@ModifiedBy [Varchar] (50) = null,
	@ModifiedDate [DateTime] = null,
	@Original_Id [UniqueIdentifier],
	@Original_Timestamp timestamp
)
AS

IF (@ModifiedDate IS NULL)
SET @ModifiedDate = getdate();

SET NOCOUNT ON;
UPDATE 
	[dbo].[GuidLinkParent] 
SET
	[Name] = @Name,
	[ModifiedBy] = @ModifiedBy,
	[ModifiedDate] = @ModifiedDate

WHERE
	[dbo].[GuidLinkParent].[Id] = @Original_Id AND
	[dbo].[GuidLinkParent].[Timestamp] = @Original_Timestamp


if (@@RowCount = 0) return;

SELECT
	[dbo].[GuidLinkParent].[id],
	[dbo].[GuidLinkParent].[name],
	[dbo].[GuidLinkParent].[CreatedBy],
	[dbo].[GuidLinkParent].[CreatedDate],
	[dbo].[GuidLinkParent].[ModifiedBy],
	[dbo].[GuidLinkParent].[ModifiedDate],
	[dbo].[GuidLinkParent].[Timestamp]
FROM 
[dbo].[GuidLinkParent]
WHERE
	[dbo].[GuidLinkParent].[Id] = @Original_Id
GO

--This SQL is generated for internal stored procedures for table [Left]

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[gen_Left_Delete]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
	drop procedure [dbo].[gen_Left_Delete]
GO

CREATE PROCEDURE [dbo].[gen_Left_Delete]
(
	@Original_LeftId [Int]
)
AS
SET NOCOUNT OFF;

DELETE FROM
	[dbo].[Left] 
WHERE 
	[LeftId] = @Original_LeftId;

if (@@RowCount = 0) return;


GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[gen_Left_Insert]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
	drop procedure [dbo].[gen_Left_Insert]
GO

CREATE PROCEDURE [dbo].[gen_Left_Insert]
(
	@LeftId [Int] = null,
	@Name [VarChar] (50),
	@CreatedDate [DateTime] = null,
	@CreatedBy [Varchar] (50) = null,
	@ModifiedBy [Varchar] (50) = null

)
AS
SET NOCOUNT OFF;

if (@CreatedDate IS NULL)
SET @CreatedDate = getdate()
DECLARE @ModifiedDate [DateTime]
SET @ModifiedDate = getdate()
IF (@LeftId < 0) SET @LeftId = NULL;
if ((@LeftId IS NULL))
BEGIN
INSERT INTO [dbo].[Left]
(
	[Name],
	[CreatedDate],
	[CreatedBy],
	[ModifiedDate],
	[ModifiedBy]
)
VALUES
(
	@Name,
	@CreatedDate,
	@CreatedBy,
	@ModifiedDate,
	@ModifiedBy
);

if (@@RowCount = 0) return;

END
ELSE
BEGIN
SET identity_insert [dbo].[Left] on
INSERT INTO [dbo].[Left]
(
	[LeftId],
	[Name],
	[CreatedDate],
	[CreatedBy],
	[ModifiedDate],
	[ModifiedBy]
)
VALUES
(
	@LeftId,
	@Name,
	@CreatedDate,
	@CreatedBy,
	@ModifiedDate,
	@ModifiedBy
);

if (@@RowCount = 0) return;

SET identity_insert [dbo].[Left] off
END


SELECT 
	[dbo].[Left].[leftid],
	[dbo].[Left].[name],
	[dbo].[Left].[CreatedBy],
	[dbo].[Left].[CreatedDate],
	[dbo].[Left].[ModifiedBy],
	[dbo].[Left].[ModifiedDate],
	[dbo].[Left].[Timestamp]

FROM
[dbo].[Left]
WHERE
	[dbo].[Left].[LeftId] = SCOPE_IDENTITY();
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[gen_Left_Update]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
	drop procedure [dbo].[gen_Left_Update]
GO

CREATE PROCEDURE [dbo].[gen_Left_Update]
(
	@Name [VarChar] (50),
	@ModifiedBy [Varchar] (50) = null,
	@ModifiedDate [DateTime] = null,
	@Original_LeftId [Int],
	@Original_Timestamp timestamp
)
AS

IF (@ModifiedDate IS NULL)
SET @ModifiedDate = getdate();

SET NOCOUNT ON;
UPDATE 
	[dbo].[Left] 
SET
	[Name] = @Name,
	[ModifiedBy] = @ModifiedBy,
	[ModifiedDate] = @ModifiedDate

WHERE
	[dbo].[Left].[LeftId] = @Original_LeftId AND
	[dbo].[Left].[Timestamp] = @Original_Timestamp


if (@@RowCount = 0) return;

SELECT
	[dbo].[Left].[leftid],
	[dbo].[Left].[name],
	[dbo].[Left].[CreatedBy],
	[dbo].[Left].[CreatedDate],
	[dbo].[Left].[ModifiedBy],
	[dbo].[Left].[ModifiedDate],
	[dbo].[Left].[Timestamp]
FROM 
[dbo].[Left]
WHERE
	[dbo].[Left].[LeftId] = @Original_LeftId
GO

--This SQL is generated for internal stored procedures for table [LeftRight]

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[gen_LeftRight_Delete]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
	drop procedure [dbo].[gen_LeftRight_Delete]
GO

CREATE PROCEDURE [dbo].[gen_LeftRight_Delete]
(
	@Left_LeftId [Int] = null,--Entity Framework Required Parent Keys be passed in: Table 'Left'
	@Right_RightId [Int] = null,--Entity Framework Required Parent Keys be passed in: Table 'Right'
	@Original_FleftId [Int],
	@Original_FrightId [Int]
)
AS
SET NOCOUNT OFF;

DELETE FROM
	[dbo].[LeftRight] 
WHERE 
	[FleftId] = @Original_FleftId AND
	[FrightId] = @Original_FrightId;

if (@@RowCount = 0) return;


GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[gen_LeftRight_Insert]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
	drop procedure [dbo].[gen_LeftRight_Insert]
GO

CREATE PROCEDURE [dbo].[gen_LeftRight_Insert]
(
	@FleftId [Int],
	@FrightId [Int]

)
AS
SET NOCOUNT OFF;

INSERT INTO [dbo].[LeftRight]
(
	[FleftId],
	[FrightId]
)
VALUES
(
	@FleftId,
	@FrightId
);

if (@@RowCount = 0) return;



SELECT 
	[dbo].[LeftRight].[fleftid],
	[dbo].[LeftRight].[frightid]

FROM
[dbo].[LeftRight]
WHERE
	[dbo].[LeftRight].[FleftId] = @FleftId AND
	[dbo].[LeftRight].[FrightId] = @FrightId;
GO

--This SQL is generated for internal stored procedures for table [ManyTypes]

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[gen_ManyTypes_Delete]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
	drop procedure [dbo].[gen_ManyTypes_Delete]
GO

CREATE PROCEDURE [dbo].[gen_ManyTypes_Delete]
(
	@Original_GuidId [UniqueIdentifier]
)
AS
SET NOCOUNT OFF;

DELETE FROM
	[dbo].[ManyTypes] 
WHERE 
	[GuidId] = @Original_GuidId;

if (@@RowCount = 0) return;


GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[gen_ManyTypes_Insert]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
	drop procedure [dbo].[gen_ManyTypes_Insert]
GO

CREATE PROCEDURE [dbo].[gen_ManyTypes_Insert]
(
	@GuidId [UniqueIdentifier],
	@TestBigint [BigInt],
	@TestBinary [Binary] (100),
	@TestBit [Bit],
	@TestChar [Char] (100) = ' ',
	@TestDate [Date],
	@TestDatetime [DateTime],
	@TestDatetime2 [DateTime2],
	@TestDatetimeoffset [DateTimeOffset],
	@TestDecimal [Decimal] (20, 2),
	@TestFloat [Float],
	@TestImage [Image],
	@TestInt [Int] = 278,
	@TestMoney [Money],
	@TestNchar [NChar] (100),
	@TestNtext [NText],
	@TestNvarchar [NVarChar] (100),
	@TestReal [Real],
	@TestSmalldatetime [SmallDateTime],
	@TestSmallint [SmallInt],
	@TestSmallmoney [SmallMoney],
	@TestText [Text],
	@TestTime [Time],
	@TestTinyint [TinyInt],
	@TestUniqueidentifier [UniqueIdentifier],
	@TestVarbinary [VarBinary] (100),
	@TestVarchar [VarChar] (100),
	@TestXml [Xml],
	@Varcharmax [VarChar] (max),
	@CreatedDate [DateTime] = null,
	@CreatedBy [Varchar] (50) = null,
	@ModifiedBy [Varchar] (50) = null

)
AS
SET NOCOUNT OFF;

if (@CreatedDate IS NULL)
SET @CreatedDate = getdate()
DECLARE @ModifiedDate [DateTime]
SET @ModifiedDate = getdate()
INSERT INTO [dbo].[ManyTypes]
(
	[GuidId],
	[TestBigint],
	[TestBinary],
	[TestBit],
	[TestChar],
	[TestDate],
	[TestDatetime],
	[TestDatetime2],
	[TestDatetimeoffset],
	[TestDecimal],
	[TestFloat],
	[TestImage],
	[TestInt],
	[TestMoney],
	[TestNchar],
	[TestNtext],
	[TestNvarchar],
	[TestReal],
	[TestSmalldatetime],
	[TestSmallint],
	[TestSmallmoney],
	[TestText],
	[TestTime],
	[TestTinyint],
	[TestUniqueidentifier],
	[TestVarbinary],
	[TestVarchar],
	[TestXml],
	[Varcharmax],
	[CreatedDate],
	[CreatedBy],
	[ModifiedDate],
	[ModifiedBy]
)
VALUES
(
	@GuidId,
	@TestBigint,
	@TestBinary,
	@TestBit,
	@TestChar,
	@TestDate,
	@TestDatetime,
	@TestDatetime2,
	@TestDatetimeoffset,
	@TestDecimal,
	@TestFloat,
	@TestImage,
	@TestInt,
	@TestMoney,
	@TestNchar,
	@TestNtext,
	@TestNvarchar,
	@TestReal,
	@TestSmalldatetime,
	@TestSmallint,
	@TestSmallmoney,
	@TestText,
	@TestTime,
	@TestTinyint,
	@TestUniqueidentifier,
	@TestVarbinary,
	@TestVarchar,
	@TestXml,
	@Varcharmax,
	@CreatedDate,
	@CreatedBy,
	@ModifiedDate,
	@ModifiedBy
);

if (@@RowCount = 0) return;



SELECT 
	[dbo].[ManyTypes].[guidid],
	[dbo].[ManyTypes].[testbigint],
	[dbo].[ManyTypes].[testbinary],
	[dbo].[ManyTypes].[testbit],
	[dbo].[ManyTypes].[testcalculated],
	[dbo].[ManyTypes].[testchar],
	[dbo].[ManyTypes].[testdate],
	[dbo].[ManyTypes].[testdatetime],
	[dbo].[ManyTypes].[testdatetime2],
	[dbo].[ManyTypes].[testdatetimeoffset],
	[dbo].[ManyTypes].[testdecimal],
	[dbo].[ManyTypes].[testfloat],
	[dbo].[ManyTypes].[testimage],
	[dbo].[ManyTypes].[testint],
	[dbo].[ManyTypes].[testmoney],
	[dbo].[ManyTypes].[testnchar],
	[dbo].[ManyTypes].[testntext],
	[dbo].[ManyTypes].[testnvarchar],
	[dbo].[ManyTypes].[testreal],
	[dbo].[ManyTypes].[testsmalldatetime],
	[dbo].[ManyTypes].[testsmallint],
	[dbo].[ManyTypes].[testsmallmoney],
	[dbo].[ManyTypes].[testtext],
	[dbo].[ManyTypes].[testtime],
	[dbo].[ManyTypes].[testtinyint],
	[dbo].[ManyTypes].[testuniqueidentifier],
	[dbo].[ManyTypes].[testvarbinary],
	[dbo].[ManyTypes].[testvarchar],
	[dbo].[ManyTypes].[testxml],
	[dbo].[ManyTypes].[varcharmax],
	[dbo].[ManyTypes].[CreatedBy],
	[dbo].[ManyTypes].[CreatedDate],
	[dbo].[ManyTypes].[ModifiedBy],
	[dbo].[ManyTypes].[ModifiedDate],
	[dbo].[ManyTypes].[Timestamp]

FROM
[dbo].[ManyTypes]
WHERE
	[dbo].[ManyTypes].[GuidId] = @GuidId;
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[gen_ManyTypes__AUDIT_SELECT]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
	drop procedure [dbo].[gen_ManyTypes__AUDIT_SELECT]
GO

CREATE PROCEDURE [dbo].[gen_ManyTypes__AUDIT_SELECT]
(
 @GuidId [UniqueIdentifier],
 @__auditType [int] = null,
 @__pageOffset [int] = 1,
 @__recordsPerPage [int] = null,
 @__startDate [datetime] = null,
 @__endDate [datetime] = null
)
AS

SET NOCOUNT ON;

if(@__pageOffset < 1 ) SET @__pageOffset = 1
if((NOT (@__recordsPerPage IS NULL)) AND (@__recordsPerPage < 1 )) SET @__recordsPerPage = 1
IF (@__recordsPerPage IS NULL)
BEGIN
SELECT
	[__AUDIT__ManyTypes].[__action],
	[__AUDIT__ManyTypes].[__insertdate],
	[__AUDIT__ManyTypes].[ModifiedBy],
	[__AUDIT__ManyTypes].[GuidId],
	[__AUDIT__ManyTypes].[TestBigint],
	[__AUDIT__ManyTypes].[TestBinary],
	[__AUDIT__ManyTypes].[TestBit],
	[__AUDIT__ManyTypes].[Testcalculated],
	[__AUDIT__ManyTypes].[TestChar],
	[__AUDIT__ManyTypes].[TestDate],
	[__AUDIT__ManyTypes].[TestDatetime],
	[__AUDIT__ManyTypes].[TestDatetime2],
	[__AUDIT__ManyTypes].[TestDatetimeoffset],
	[__AUDIT__ManyTypes].[TestDecimal],
	[__AUDIT__ManyTypes].[TestFloat],
	[__AUDIT__ManyTypes].[TestInt],
	[__AUDIT__ManyTypes].[TestMoney],
	[__AUDIT__ManyTypes].[TestNchar],
	[__AUDIT__ManyTypes].[TestNvarchar],
	[__AUDIT__ManyTypes].[TestReal],
	[__AUDIT__ManyTypes].[TestSmalldatetime],
	[__AUDIT__ManyTypes].[TestSmallint],
	[__AUDIT__ManyTypes].[TestSmallmoney],
	[__AUDIT__ManyTypes].[TestTime],
	[__AUDIT__ManyTypes].[TestTinyint],
	[__AUDIT__ManyTypes].[TestUniqueidentifier],
	[__AUDIT__ManyTypes].[TestVarbinary],
	[__AUDIT__ManyTypes].[TestVarchar],
	[__AUDIT__ManyTypes].[TestXml],
	[__AUDIT__ManyTypes].[Varcharmax]
FROM
	[__AUDIT__ManyTypes]
WHERE
	((@__auditType IS NULL) OR ([__AUDIT__ManyTypes].__action = @__auditType)) AND
	((@__startDate IS NULL) OR ([__AUDIT__ManyTypes].__insertdate >= @__startDate)) AND
	((@__endDate IS NULL) OR ([__AUDIT__ManyTypes].__insertdate < @__endDate)) AND
	(@GuidId = [__AUDIT__ManyTypes].GuidId)
	ORDER BY [__AUDIT__ManyTypes].__insertdate DESC, [__AUDIT__ManyTypes].__rowid DESC

END
ELSE
BEGIN
SELECT
	[__TempTable].[__action],
	[__TempTable].[__insertdate],
	[__TempTable].[ModifiedBy],
	[__TempTable].[GuidId],
	[__TempTable].[TestBigint],
	[__TempTable].[TestBinary],
	[__TempTable].[TestBit],
	[__TempTable].[Testcalculated],
	[__TempTable].[TestChar],
	[__TempTable].[TestDate],
	[__TempTable].[TestDatetime],
	[__TempTable].[TestDatetime2],
	[__TempTable].[TestDatetimeoffset],
	[__TempTable].[TestDecimal],
	[__TempTable].[TestFloat],
	[__TempTable].[TestInt],
	[__TempTable].[TestMoney],
	[__TempTable].[TestNchar],
	[__TempTable].[TestNvarchar],
	[__TempTable].[TestReal],
	[__TempTable].[TestSmalldatetime],
	[__TempTable].[TestSmallint],
	[__TempTable].[TestSmallmoney],
	[__TempTable].[TestTime],
	[__TempTable].[TestTinyint],
	[__TempTable].[TestUniqueidentifier],
	[__TempTable].[TestVarbinary],
	[__TempTable].[TestVarchar],
	[__TempTable].[TestXml],
	[__TempTable].[Varcharmax]
FROM
	(SELECT  ROW_NUMBER() OVER (ORDER BY [__AUDIT__ManyTypes].__insertdate DESC, [__AUDIT__ManyTypes].__rowid DESC)
	AS __row,
	[__AUDIT__ManyTypes].[__action],
	[__AUDIT__ManyTypes].[__insertdate],
	[__AUDIT__ManyTypes].ModifiedBy,
	[__AUDIT__ManyTypes].[GuidId],
	[__AUDIT__ManyTypes].[TestBigint],
	[__AUDIT__ManyTypes].[TestBinary],
	[__AUDIT__ManyTypes].[TestBit],
	[__AUDIT__ManyTypes].[Testcalculated],
	[__AUDIT__ManyTypes].[TestChar],
	[__AUDIT__ManyTypes].[TestDate],
	[__AUDIT__ManyTypes].[TestDatetime],
	[__AUDIT__ManyTypes].[TestDatetime2],
	[__AUDIT__ManyTypes].[TestDatetimeoffset],
	[__AUDIT__ManyTypes].[TestDecimal],
	[__AUDIT__ManyTypes].[TestFloat],
	[__AUDIT__ManyTypes].[TestInt],
	[__AUDIT__ManyTypes].[TestMoney],
	[__AUDIT__ManyTypes].[TestNchar],
	[__AUDIT__ManyTypes].[TestNvarchar],
	[__AUDIT__ManyTypes].[TestReal],
	[__AUDIT__ManyTypes].[TestSmalldatetime],
	[__AUDIT__ManyTypes].[TestSmallint],
	[__AUDIT__ManyTypes].[TestSmallmoney],
	[__AUDIT__ManyTypes].[TestTime],
	[__AUDIT__ManyTypes].[TestTinyint],
	[__AUDIT__ManyTypes].[TestUniqueidentifier],
	[__AUDIT__ManyTypes].[TestVarbinary],
	[__AUDIT__ManyTypes].[TestVarchar],
	[__AUDIT__ManyTypes].[TestXml],
	[__AUDIT__ManyTypes].[Varcharmax]
	FROM 
		[__AUDIT__ManyTypes]
	WHERE
		((@__auditType IS NULL) OR ([__AUDIT__ManyTypes].__action = @__auditType)) AND
	((@__startDate IS NULL) OR ([__AUDIT__ManyTypes].__insertdate >= @__startDate)) AND
	((@__endDate IS NULL) OR ([__AUDIT__ManyTypes].__insertdate < @__endDate)) AND
		(@GuidId = [__AUDIT__ManyTypes].GuidId)
	)
	AS [__TempTable]
WHERE
	__row >= (((@__pageOffset-1)*@__recordsPerPage)+1) AND __row <= (@__pageOffset*@__recordsPerPage)

END
return (select COUNT(*) 
	FROM [__AUDIT__ManyTypes] WHERE
		((@__auditType IS NULL) OR ([__AUDIT__ManyTypes].__action = @__auditType)) AND
	((@__startDate IS NULL) OR ([__AUDIT__ManyTypes].__insertdate >= @__startDate)) AND
	((@__endDate IS NULL) OR ([__AUDIT__ManyTypes].__insertdate < @__endDate)) AND
		(@GuidId = [__AUDIT__ManyTypes].GuidId)
		)

GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[gen_ManyTypes__AUDIT_SELECT_ALL]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
	drop procedure [dbo].[gen_ManyTypes__AUDIT_SELECT_ALL]
GO

CREATE PROCEDURE [dbo].[gen_ManyTypes__AUDIT_SELECT_ALL]
(
 @__auditType [int] = null,
 @__pageOffset [int] = 1,
 @__recordsPerPage [int] = null,
 @__startDate [datetime] = null,
 @__endDate [datetime] = null
)
AS

SET NOCOUNT ON;

if(@__pageOffset < 1 ) SET @__pageOffset = 1
if((NOT (@__recordsPerPage IS NULL)) AND (@__recordsPerPage < 1 )) SET @__recordsPerPage = 1
IF (@__recordsPerPage IS NULL)
BEGIN
SELECT
	[__AUDIT__ManyTypes].[__action],
	[__AUDIT__ManyTypes].[__insertdate],
	[__AUDIT__ManyTypes].[ModifiedBy],
	[__AUDIT__ManyTypes].[GuidId],
	[__AUDIT__ManyTypes].[TestBigint],
	[__AUDIT__ManyTypes].[TestBinary],
	[__AUDIT__ManyTypes].[TestBit],
	[__AUDIT__ManyTypes].[Testcalculated],
	[__AUDIT__ManyTypes].[TestChar],
	[__AUDIT__ManyTypes].[TestDate],
	[__AUDIT__ManyTypes].[TestDatetime],
	[__AUDIT__ManyTypes].[TestDatetime2],
	[__AUDIT__ManyTypes].[TestDatetimeoffset],
	[__AUDIT__ManyTypes].[TestDecimal],
	[__AUDIT__ManyTypes].[TestFloat],
	[__AUDIT__ManyTypes].[TestInt],
	[__AUDIT__ManyTypes].[TestMoney],
	[__AUDIT__ManyTypes].[TestNchar],
	[__AUDIT__ManyTypes].[TestNvarchar],
	[__AUDIT__ManyTypes].[TestReal],
	[__AUDIT__ManyTypes].[TestSmalldatetime],
	[__AUDIT__ManyTypes].[TestSmallint],
	[__AUDIT__ManyTypes].[TestSmallmoney],
	[__AUDIT__ManyTypes].[TestTime],
	[__AUDIT__ManyTypes].[TestTinyint],
	[__AUDIT__ManyTypes].[TestUniqueidentifier],
	[__AUDIT__ManyTypes].[TestVarbinary],
	[__AUDIT__ManyTypes].[TestVarchar],
	[__AUDIT__ManyTypes].[TestXml],
	[__AUDIT__ManyTypes].[Varcharmax]
FROM
	[__AUDIT__ManyTypes]
WHERE
	((@__auditType IS NULL) OR ([__AUDIT__ManyTypes].__action = @__auditType)) AND
	((@__startDate IS NULL) OR ([__AUDIT__ManyTypes].__insertdate >= @__startDate)) AND
	((@__endDate IS NULL) OR ([__AUDIT__ManyTypes].__insertdate < @__endDate))
	ORDER BY [__AUDIT__ManyTypes].__insertdate DESC, [__AUDIT__ManyTypes].__rowid DESC

END
ELSE
BEGIN
SELECT
	[__TempTable].[__action],
	[__TempTable].[__insertdate],
	[__TempTable].[ModifiedBy],
	[__TempTable].[GuidId],
	[__TempTable].[TestBigint],
	[__TempTable].[TestBinary],
	[__TempTable].[TestBit],
	[__TempTable].[Testcalculated],
	[__TempTable].[TestChar],
	[__TempTable].[TestDate],
	[__TempTable].[TestDatetime],
	[__TempTable].[TestDatetime2],
	[__TempTable].[TestDatetimeoffset],
	[__TempTable].[TestDecimal],
	[__TempTable].[TestFloat],
	[__TempTable].[TestInt],
	[__TempTable].[TestMoney],
	[__TempTable].[TestNchar],
	[__TempTable].[TestNvarchar],
	[__TempTable].[TestReal],
	[__TempTable].[TestSmalldatetime],
	[__TempTable].[TestSmallint],
	[__TempTable].[TestSmallmoney],
	[__TempTable].[TestTime],
	[__TempTable].[TestTinyint],
	[__TempTable].[TestUniqueidentifier],
	[__TempTable].[TestVarbinary],
	[__TempTable].[TestVarchar],
	[__TempTable].[TestXml],
	[__TempTable].[Varcharmax]
FROM
	(SELECT  ROW_NUMBER() OVER (ORDER BY [__AUDIT__ManyTypes].__insertdate DESC, [__AUDIT__ManyTypes].__rowid DESC)
	AS __row,
	[__AUDIT__ManyTypes].[__action],
	[__AUDIT__ManyTypes].[__insertdate],
	[__AUDIT__ManyTypes].ModifiedBy,
	[__AUDIT__ManyTypes].[GuidId],
	[__AUDIT__ManyTypes].[TestBigint],
	[__AUDIT__ManyTypes].[TestBinary],
	[__AUDIT__ManyTypes].[TestBit],
	[__AUDIT__ManyTypes].[Testcalculated],
	[__AUDIT__ManyTypes].[TestChar],
	[__AUDIT__ManyTypes].[TestDate],
	[__AUDIT__ManyTypes].[TestDatetime],
	[__AUDIT__ManyTypes].[TestDatetime2],
	[__AUDIT__ManyTypes].[TestDatetimeoffset],
	[__AUDIT__ManyTypes].[TestDecimal],
	[__AUDIT__ManyTypes].[TestFloat],
	[__AUDIT__ManyTypes].[TestInt],
	[__AUDIT__ManyTypes].[TestMoney],
	[__AUDIT__ManyTypes].[TestNchar],
	[__AUDIT__ManyTypes].[TestNvarchar],
	[__AUDIT__ManyTypes].[TestReal],
	[__AUDIT__ManyTypes].[TestSmalldatetime],
	[__AUDIT__ManyTypes].[TestSmallint],
	[__AUDIT__ManyTypes].[TestSmallmoney],
	[__AUDIT__ManyTypes].[TestTime],
	[__AUDIT__ManyTypes].[TestTinyint],
	[__AUDIT__ManyTypes].[TestUniqueidentifier],
	[__AUDIT__ManyTypes].[TestVarbinary],
	[__AUDIT__ManyTypes].[TestVarchar],
	[__AUDIT__ManyTypes].[TestXml],
	[__AUDIT__ManyTypes].[Varcharmax]
	FROM 
		[__AUDIT__ManyTypes]
	WHERE
		((@__auditType IS NULL) OR ([__AUDIT__ManyTypes].__action = @__auditType)) AND
	((@__startDate IS NULL) OR ([__AUDIT__ManyTypes].__insertdate >= @__startDate)) AND
	((@__endDate IS NULL) OR ([__AUDIT__ManyTypes].__insertdate < @__endDate))
	)
	AS [__TempTable]
WHERE
	__row >= (((@__pageOffset-1)*@__recordsPerPage)+1) AND __row <= (@__pageOffset*@__recordsPerPage)

END
return (select COUNT(*) 
	FROM [__AUDIT__ManyTypes] WHERE
		((@__auditType IS NULL) OR ([__AUDIT__ManyTypes].__action = @__auditType)) AND
	((@__startDate IS NULL) OR ([__AUDIT__ManyTypes].__insertdate >= @__startDate)) AND
	((@__endDate IS NULL) OR ([__AUDIT__ManyTypes].__insertdate < @__endDate))
		)

GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[gen_ManyTypes_Update]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
	drop procedure [dbo].[gen_ManyTypes_Update]
GO

CREATE PROCEDURE [dbo].[gen_ManyTypes_Update]
(
	@TestBigint [BigInt],
	@TestBinary [Binary] (100),
	@TestBit [Bit],
	@TestChar [Char] (100) = ' ',
	@TestDate [Date],
	@TestDatetime [DateTime],
	@TestDatetime2 [DateTime2],
	@TestDatetimeoffset [DateTimeOffset],
	@TestDecimal [Decimal] (20, 2),
	@TestFloat [Float],
	@TestImage [Image],
	@TestInt [Int] = 278,
	@TestMoney [Money],
	@TestNchar [NChar] (100),
	@TestNtext [NText],
	@TestNvarchar [NVarChar] (100),
	@TestReal [Real],
	@TestSmalldatetime [SmallDateTime],
	@TestSmallint [SmallInt],
	@TestSmallmoney [SmallMoney],
	@TestText [Text],
	@TestTime [Time],
	@TestTinyint [TinyInt],
	@TestUniqueidentifier [UniqueIdentifier],
	@TestVarbinary [VarBinary] (100),
	@TestVarchar [VarChar] (100),
	@TestXml [Xml],
	@Varcharmax [VarChar] (max),
	@ModifiedBy [Varchar] (50) = null,
	@ModifiedDate [DateTime] = null,
	@Original_GuidId [UniqueIdentifier],
	@Original_Timestamp timestamp
)
AS

IF (@ModifiedDate IS NULL)
SET @ModifiedDate = getdate();

SET NOCOUNT ON;
UPDATE 
	[dbo].[ManyTypes] 
SET
	[TestBigint] = @TestBigint,
	[TestBinary] = @TestBinary,
	[TestBit] = @TestBit,
	[TestChar] = @TestChar,
	[TestDate] = @TestDate,
	[TestDatetime] = @TestDatetime,
	[TestDatetime2] = @TestDatetime2,
	[TestDatetimeoffset] = @TestDatetimeoffset,
	[TestDecimal] = @TestDecimal,
	[TestFloat] = @TestFloat,
	[TestImage] = @TestImage,
	[TestInt] = @TestInt,
	[TestMoney] = @TestMoney,
	[TestNchar] = @TestNchar,
	[TestNtext] = @TestNtext,
	[TestNvarchar] = @TestNvarchar,
	[TestReal] = @TestReal,
	[TestSmalldatetime] = @TestSmalldatetime,
	[TestSmallint] = @TestSmallint,
	[TestSmallmoney] = @TestSmallmoney,
	[TestText] = @TestText,
	[TestTime] = @TestTime,
	[TestTinyint] = @TestTinyint,
	[TestUniqueidentifier] = @TestUniqueidentifier,
	[TestVarbinary] = @TestVarbinary,
	[TestVarchar] = @TestVarchar,
	[TestXml] = @TestXml,
	[Varcharmax] = @Varcharmax,
	[ModifiedBy] = @ModifiedBy,
	[ModifiedDate] = @ModifiedDate

WHERE
	[dbo].[ManyTypes].[GuidId] = @Original_GuidId AND
	[dbo].[ManyTypes].[Timestamp] = @Original_Timestamp


if (@@RowCount = 0) return;

SELECT
	[dbo].[ManyTypes].[guidid],
	[dbo].[ManyTypes].[testbigint],
	[dbo].[ManyTypes].[testbinary],
	[dbo].[ManyTypes].[testbit],
	[dbo].[ManyTypes].[testcalculated],
	[dbo].[ManyTypes].[testchar],
	[dbo].[ManyTypes].[testdate],
	[dbo].[ManyTypes].[testdatetime],
	[dbo].[ManyTypes].[testdatetime2],
	[dbo].[ManyTypes].[testdatetimeoffset],
	[dbo].[ManyTypes].[testdecimal],
	[dbo].[ManyTypes].[testfloat],
	[dbo].[ManyTypes].[testimage],
	[dbo].[ManyTypes].[testint],
	[dbo].[ManyTypes].[testmoney],
	[dbo].[ManyTypes].[testnchar],
	[dbo].[ManyTypes].[testntext],
	[dbo].[ManyTypes].[testnvarchar],
	[dbo].[ManyTypes].[testreal],
	[dbo].[ManyTypes].[testsmalldatetime],
	[dbo].[ManyTypes].[testsmallint],
	[dbo].[ManyTypes].[testsmallmoney],
	[dbo].[ManyTypes].[testtext],
	[dbo].[ManyTypes].[testtime],
	[dbo].[ManyTypes].[testtinyint],
	[dbo].[ManyTypes].[testuniqueidentifier],
	[dbo].[ManyTypes].[testvarbinary],
	[dbo].[ManyTypes].[testvarchar],
	[dbo].[ManyTypes].[testxml],
	[dbo].[ManyTypes].[varcharmax],
	[dbo].[ManyTypes].[CreatedBy],
	[dbo].[ManyTypes].[CreatedDate],
	[dbo].[ManyTypes].[ModifiedBy],
	[dbo].[ManyTypes].[ModifiedDate],
	[dbo].[ManyTypes].[Timestamp]
FROM 
[dbo].[ManyTypes]
WHERE
	[dbo].[ManyTypes].[GuidId] = @Original_GuidId
GO

--This SQL is generated for internal stored procedures for table [OneToOneChild]

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[gen_OneToOneChild_Delete]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
	drop procedure [dbo].[gen_OneToOneChild_Delete]
GO

CREATE PROCEDURE [dbo].[gen_OneToOneChild_Delete]
(
	@OneToOneParent_UserId [Int] = null,--Entity Framework Required Parent Keys be passed in: Table 'OneToOneParent'
	@Original_UserId [Int]
)
AS
SET NOCOUNT OFF;

DELETE FROM
	[dbo].[OneToOneChild] 
WHERE 
	[UserId] = @Original_UserId;

if (@@RowCount = 0) return;


GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[gen_OneToOneChild_Insert]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
	drop procedure [dbo].[gen_OneToOneChild_Insert]
GO

CREATE PROCEDURE [dbo].[gen_OneToOneChild_Insert]
(
	@LastName [VarChar] (50) = null,
	@UserId [Int]

)
AS
SET NOCOUNT OFF;

INSERT INTO [dbo].[OneToOneChild]
(
	[LastName],
	[UserId]
)
VALUES
(
	@LastName,
	@UserId
);

if (@@RowCount = 0) return;



SELECT 
	[dbo].[OneToOneChild].[lastname],
	[dbo].[OneToOneChild].[userid]

FROM
[dbo].[OneToOneChild]
WHERE
	[dbo].[OneToOneChild].[UserId] = @UserId;
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[gen_OneToOneChild_Update]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
	drop procedure [dbo].[gen_OneToOneChild_Update]
GO

CREATE PROCEDURE [dbo].[gen_OneToOneChild_Update]
(
	@LastName [VarChar] (50) = null,
	@Original_UserId [Int]
)
AS

SET NOCOUNT ON;
UPDATE 
	[dbo].[OneToOneChild] 
SET
	[LastName] = @LastName

WHERE
	[dbo].[OneToOneChild].[UserId] = @Original_UserId


if (@@RowCount = 0) return;

SELECT
	[dbo].[OneToOneChild].[lastname],
	[dbo].[OneToOneChild].[userid]
FROM 
[dbo].[OneToOneChild]
WHERE
	[dbo].[OneToOneChild].[UserId] = @Original_UserId
GO

--This SQL is generated for internal stored procedures for table [OneToOneParent]

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[gen_OneToOneParent_Delete]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
	drop procedure [dbo].[gen_OneToOneParent_Delete]
GO

CREATE PROCEDURE [dbo].[gen_OneToOneParent_Delete]
(
	@Original_UserId [Int]
)
AS
SET NOCOUNT OFF;

DELETE FROM
	[dbo].[OneToOneParent] 
WHERE 
	[UserId] = @Original_UserId;

if (@@RowCount = 0) return;


GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[gen_OneToOneParent_Insert]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
	drop procedure [dbo].[gen_OneToOneParent_Insert]
GO

CREATE PROCEDURE [dbo].[gen_OneToOneParent_Insert]
(
	@FirstName [VarChar] (50) = null,
	@UserId [Int] = null

)
AS
SET NOCOUNT OFF;

IF (@UserId < 0) SET @UserId = NULL;
if ((@UserId IS NULL))
BEGIN
INSERT INTO [dbo].[OneToOneParent]
(
	[FirstName]
)
VALUES
(
	@FirstName
);

if (@@RowCount = 0) return;

END
ELSE
BEGIN
SET identity_insert [dbo].[OneToOneParent] on
INSERT INTO [dbo].[OneToOneParent]
(
	[UserId],
	[FirstName]
)
VALUES
(
	@UserId,
	@FirstName
);

if (@@RowCount = 0) return;

SET identity_insert [dbo].[OneToOneParent] off
END


SELECT 
	[dbo].[OneToOneParent].[firstname],
	[dbo].[OneToOneParent].[userid]

FROM
[dbo].[OneToOneParent]
WHERE
	[dbo].[OneToOneParent].[UserId] = SCOPE_IDENTITY();
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[gen_OneToOneParent_Update]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
	drop procedure [dbo].[gen_OneToOneParent_Update]
GO

CREATE PROCEDURE [dbo].[gen_OneToOneParent_Update]
(
	@FirstName [VarChar] (50) = null,
	@Original_UserId [Int]
)
AS

SET NOCOUNT ON;
UPDATE 
	[dbo].[OneToOneParent] 
SET
	[FirstName] = @FirstName

WHERE
	[dbo].[OneToOneParent].[UserId] = @Original_UserId


if (@@RowCount = 0) return;

SELECT
	[dbo].[OneToOneParent].[firstname],
	[dbo].[OneToOneParent].[userid]
FROM 
[dbo].[OneToOneParent]
WHERE
	[dbo].[OneToOneParent].[UserId] = @Original_UserId
GO

--This SQL is generated for internal stored procedures for table [Parent]

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[gen_Parent_Delete]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
	drop procedure [dbo].[gen_Parent_Delete]
GO

CREATE PROCEDURE [dbo].[gen_Parent_Delete]
(
	@ParentType_ParentTypeId [Int] = null,--Entity Framework Required Parent Keys be passed in: Table 'ParentType'
	@Original_ParentId [Int]
)
AS
SET NOCOUNT OFF;

DELETE FROM
	[dbo].[Parent] 
WHERE 
	[ParentId] = @Original_ParentId;

if (@@RowCount = 0) return;


GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[gen_Parent_Insert]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
	drop procedure [dbo].[gen_Parent_Insert]
GO

CREATE PROCEDURE [dbo].[gen_Parent_Insert]
(
	@FparentTypeId [Int],
	@Name [VarChar] (100),
	@ParentId [Int] = null,
	@CreatedDate [DateTime] = null,
	@CreatedBy [Varchar] (50) = null,
	@ModifiedBy [Varchar] (50) = null

)
AS
SET NOCOUNT OFF;

if (@CreatedDate IS NULL)
SET @CreatedDate = getdate()
DECLARE @ModifiedDate [DateTime]
SET @ModifiedDate = getdate()
IF (@ParentId < 0) SET @ParentId = NULL;
if ((@ParentId IS NULL))
BEGIN
INSERT INTO [dbo].[Parent]
(
	[FparentTypeId],
	[Name],
	[CreatedDate],
	[CreatedBy],
	[ModifiedDate],
	[ModifiedBy]
)
VALUES
(
	@FparentTypeId,
	@Name,
	@CreatedDate,
	@CreatedBy,
	@ModifiedDate,
	@ModifiedBy
);

if (@@RowCount = 0) return;

END
ELSE
BEGIN
SET identity_insert [dbo].[Parent] on
INSERT INTO [dbo].[Parent]
(
	[ParentId],
	[FparentTypeId],
	[Name],
	[CreatedDate],
	[CreatedBy],
	[ModifiedDate],
	[ModifiedBy]
)
VALUES
(
	@ParentId,
	@FparentTypeId,
	@Name,
	@CreatedDate,
	@CreatedBy,
	@ModifiedDate,
	@ModifiedBy
);

if (@@RowCount = 0) return;

SET identity_insert [dbo].[Parent] off
END


SELECT 
	[dbo].[Parent].[fparenttypeid],
	[dbo].[Parent].[name],
	[dbo].[Parent].[parentid],
	[dbo].[Parent].[CreatedBy],
	[dbo].[Parent].[CreatedDate],
	[dbo].[Parent].[ModifiedBy],
	[dbo].[Parent].[ModifiedDate],
	[dbo].[Parent].[Timestamp]

FROM
[dbo].[Parent]
WHERE
	[dbo].[Parent].[ParentId] = SCOPE_IDENTITY();
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[gen_Parent_Update]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
	drop procedure [dbo].[gen_Parent_Update]
GO

CREATE PROCEDURE [dbo].[gen_Parent_Update]
(
	@FparentTypeId [Int],
	@Name [VarChar] (100),
	@ModifiedBy [Varchar] (50) = null,
	@ModifiedDate [DateTime] = null,
	@Original_ParentId [Int],
	@Original_Timestamp timestamp
)
AS

IF (@ModifiedDate IS NULL)
SET @ModifiedDate = getdate();

SET NOCOUNT ON;
UPDATE 
	[dbo].[Parent] 
SET
	[FparentTypeId] = @FparentTypeId,
	[Name] = @Name,
	[ModifiedBy] = @ModifiedBy,
	[ModifiedDate] = @ModifiedDate

WHERE
	[dbo].[Parent].[ParentId] = @Original_ParentId AND
	[dbo].[Parent].[Timestamp] = @Original_Timestamp


if (@@RowCount = 0) return;

SELECT
	[dbo].[Parent].[fparenttypeid],
	[dbo].[Parent].[name],
	[dbo].[Parent].[parentid],
	[dbo].[Parent].[CreatedBy],
	[dbo].[Parent].[CreatedDate],
	[dbo].[Parent].[ModifiedBy],
	[dbo].[Parent].[ModifiedDate],
	[dbo].[Parent].[Timestamp]
FROM 
[dbo].[Parent]
WHERE
	[dbo].[Parent].[ParentId] = @Original_ParentId
GO

--This SQL is generated for internal stored procedures for table [ParentType]

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[gen_ParentType_Delete]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
	drop procedure [dbo].[gen_ParentType_Delete]
GO

CREATE PROCEDURE [dbo].[gen_ParentType_Delete]
(
	@Original_ParentTypeId [Int]
)
AS
SET NOCOUNT OFF;

GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[gen_ParentType_Insert]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
	drop procedure [dbo].[gen_ParentType_Insert]
GO

CREATE PROCEDURE [dbo].[gen_ParentType_Insert]
(
	@Name [VarChar] (50),
	@ParentTypeId [Int] = null,
	@CreatedDate [DateTime] = null,
	@CreatedBy [Varchar] (50) = null,
	@ModifiedBy [Varchar] (50) = null

)
AS
SET NOCOUNT OFF;


SELECT 
	[dbo].[ParentType].[name],
	[dbo].[ParentType].[parenttypeid],
	[dbo].[ParentType].[CreatedBy],
	[dbo].[ParentType].[CreatedDate],
	[dbo].[ParentType].[ModifiedBy],
	[dbo].[ParentType].[ModifiedDate],
	[dbo].[ParentType].[Timestamp]

FROM
[dbo].[ParentType]
WHERE
	[dbo].[ParentType].[ParentTypeId] = SCOPE_IDENTITY();
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[gen_ParentType_Update]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
	drop procedure [dbo].[gen_ParentType_Update]
GO

CREATE PROCEDURE [dbo].[gen_ParentType_Update]
(
	@Name [VarChar] (50),
	@ModifiedBy [Varchar] (50) = null,
	@ModifiedDate [DateTime] = null,
	@Original_ParentTypeId [Int],
	@Original_Timestamp timestamp
)
AS

IF (@ModifiedDate IS NULL)
SET @ModifiedDate = getdate();

SET NOCOUNT ON;
SELECT
	[dbo].[ParentType].[name],
	[dbo].[ParentType].[parenttypeid],
	[dbo].[ParentType].[CreatedBy],
	[dbo].[ParentType].[CreatedDate],
	[dbo].[ParentType].[ModifiedBy],
	[dbo].[ParentType].[ModifiedDate],
	[dbo].[ParentType].[Timestamp]
FROM 
[dbo].[ParentType]
WHERE
	[dbo].[ParentType].[ParentTypeId] = @Original_ParentTypeId
GO

--This SQL is generated for internal stored procedures for table [Right]

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[gen_Right_Delete]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
	drop procedure [dbo].[gen_Right_Delete]
GO

CREATE PROCEDURE [dbo].[gen_Right_Delete]
(
	@Original_RightId [Int]
)
AS
SET NOCOUNT OFF;

DELETE FROM
	[dbo].[Right] 
WHERE 
	[RightId] = @Original_RightId;

if (@@RowCount = 0) return;


GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[gen_Right_Insert]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
	drop procedure [dbo].[gen_Right_Insert]
GO

CREATE PROCEDURE [dbo].[gen_Right_Insert]
(
	@Name [VarChar] (50),
	@RightId [Int] = null,
	@CreatedDate [DateTime] = null,
	@CreatedBy [Varchar] (50) = null,
	@ModifiedBy [Varchar] (50) = null

)
AS
SET NOCOUNT OFF;

if (@CreatedDate IS NULL)
SET @CreatedDate = getdate()
DECLARE @ModifiedDate [DateTime]
SET @ModifiedDate = getdate()
IF (@RightId < 0) SET @RightId = NULL;
if ((@RightId IS NULL))
BEGIN
INSERT INTO [dbo].[Right]
(
	[Name],
	[CreatedDate],
	[CreatedBy],
	[ModifiedDate],
	[ModifiedBy]
)
VALUES
(
	@Name,
	@CreatedDate,
	@CreatedBy,
	@ModifiedDate,
	@ModifiedBy
);

if (@@RowCount = 0) return;

END
ELSE
BEGIN
SET identity_insert [dbo].[Right] on
INSERT INTO [dbo].[Right]
(
	[RightId],
	[Name],
	[CreatedDate],
	[CreatedBy],
	[ModifiedDate],
	[ModifiedBy]
)
VALUES
(
	@RightId,
	@Name,
	@CreatedDate,
	@CreatedBy,
	@ModifiedDate,
	@ModifiedBy
);

if (@@RowCount = 0) return;

SET identity_insert [dbo].[Right] off
END


SELECT 
	[dbo].[Right].[name],
	[dbo].[Right].[rightid],
	[dbo].[Right].[CreatedBy],
	[dbo].[Right].[CreatedDate],
	[dbo].[Right].[ModifiedBy],
	[dbo].[Right].[ModifiedDate],
	[dbo].[Right].[Timestamp]

FROM
[dbo].[Right]
WHERE
	[dbo].[Right].[RightId] = SCOPE_IDENTITY();
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[gen_Right_Update]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
	drop procedure [dbo].[gen_Right_Update]
GO

CREATE PROCEDURE [dbo].[gen_Right_Update]
(
	@Name [VarChar] (50),
	@ModifiedBy [Varchar] (50) = null,
	@ModifiedDate [DateTime] = null,
	@Original_RightId [Int],
	@Original_Timestamp timestamp
)
AS

IF (@ModifiedDate IS NULL)
SET @ModifiedDate = getdate();

SET NOCOUNT ON;
UPDATE 
	[dbo].[Right] 
SET
	[Name] = @Name,
	[ModifiedBy] = @ModifiedBy,
	[ModifiedDate] = @ModifiedDate

WHERE
	[dbo].[Right].[RightId] = @Original_RightId AND
	[dbo].[Right].[Timestamp] = @Original_Timestamp


if (@@RowCount = 0) return;

SELECT
	[dbo].[Right].[name],
	[dbo].[Right].[rightid],
	[dbo].[Right].[CreatedBy],
	[dbo].[Right].[CreatedDate],
	[dbo].[Right].[ModifiedBy],
	[dbo].[Right].[ModifiedDate],
	[dbo].[Right].[Timestamp]
FROM 
[dbo].[Right]
WHERE
	[dbo].[Right].[RightId] = @Original_RightId
GO

--This SQL is generated for internal stored procedures for table [Simple]

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[gen_Simple_Delete]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
	drop procedure [dbo].[gen_Simple_Delete]
GO

CREATE PROCEDURE [dbo].[gen_Simple_Delete]
(
	@Original_SimpleId [Int]
)
AS
SET NOCOUNT OFF;

DELETE FROM
	[dbo].[Simple] 
WHERE 
	[SimpleId] = @Original_SimpleId;

if (@@RowCount = 0) return;


GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[gen_Simple_Insert]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
	drop procedure [dbo].[gen_Simple_Insert]
GO

CREATE PROCEDURE [dbo].[gen_Simple_Insert]
(
	@Name [VarChar] (50) = null,
	@NewInt [Int] = 0,
	@SimpleId [Int] = null,
	@CreatedDate [DateTime] = null,
	@CreatedBy [Varchar] (50) = null,
	@ModifiedBy [Varchar] (50) = null

)
AS
SET NOCOUNT OFF;

if (@CreatedDate IS NULL)
SET @CreatedDate = getdate()
DECLARE @ModifiedDate [DateTime]
SET @ModifiedDate = getdate()
IF (@SimpleId < 0) SET @SimpleId = NULL;
if ((@SimpleId IS NULL))
BEGIN
INSERT INTO [dbo].[Simple]
(
	[Name],
	[NewInt],
	[CreatedDate],
	[CreatedBy],
	[ModifiedDate],
	[ModifiedBy]
)
VALUES
(
	@Name,
	@NewInt,
	@CreatedDate,
	@CreatedBy,
	@ModifiedDate,
	@ModifiedBy
);

if (@@RowCount = 0) return;

END
ELSE
BEGIN
SET identity_insert [dbo].[Simple] on
INSERT INTO [dbo].[Simple]
(
	[SimpleId],
	[Name],
	[NewInt],
	[CreatedDate],
	[CreatedBy],
	[ModifiedDate],
	[ModifiedBy]
)
VALUES
(
	@SimpleId,
	@Name,
	@NewInt,
	@CreatedDate,
	@CreatedBy,
	@ModifiedDate,
	@ModifiedBy
);

if (@@RowCount = 0) return;

SET identity_insert [dbo].[Simple] off
END


SELECT 
	[dbo].[Simple].[myreadonly],
	[dbo].[Simple].[name],
	[dbo].[Simple].[newint],
	[dbo].[Simple].[simpleid],
	[dbo].[Simple].[CreatedBy],
	[dbo].[Simple].[CreatedDate],
	[dbo].[Simple].[ModifiedBy],
	[dbo].[Simple].[ModifiedDate],
	[dbo].[Simple].[Timestamp]

FROM
[dbo].[Simple]
WHERE
	[dbo].[Simple].[SimpleId] = SCOPE_IDENTITY();
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[gen_Simple__AUDIT_SELECT]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
	drop procedure [dbo].[gen_Simple__AUDIT_SELECT]
GO

CREATE PROCEDURE [dbo].[gen_Simple__AUDIT_SELECT]
(
 @SimpleId [Int],
 @__auditType [int] = null,
 @__pageOffset [int] = 1,
 @__recordsPerPage [int] = null,
 @__startDate [datetime] = null,
 @__endDate [datetime] = null
)
AS

SET NOCOUNT ON;

if(@__pageOffset < 1 ) SET @__pageOffset = 1
if((NOT (@__recordsPerPage IS NULL)) AND (@__recordsPerPage < 1 )) SET @__recordsPerPage = 1
IF (@__recordsPerPage IS NULL)
BEGIN
SELECT
	[__AUDIT__Simple].[__action],
	[__AUDIT__Simple].[__insertdate],
	[__AUDIT__Simple].[ModifiedBy],
	[__AUDIT__Simple].[MyReadonly],
	[__AUDIT__Simple].[Name],
	[__AUDIT__Simple].[NewInt],
	[__AUDIT__Simple].[SimpleId]
FROM
	[__AUDIT__Simple]
WHERE
	((@__auditType IS NULL) OR ([__AUDIT__Simple].__action = @__auditType)) AND
	((@__startDate IS NULL) OR ([__AUDIT__Simple].__insertdate >= @__startDate)) AND
	((@__endDate IS NULL) OR ([__AUDIT__Simple].__insertdate < @__endDate)) AND
	(@SimpleId = [__AUDIT__Simple].SimpleId)
	ORDER BY [__AUDIT__Simple].__insertdate DESC, [__AUDIT__Simple].__rowid DESC

END
ELSE
BEGIN
SELECT
	[__TempTable].[__action],
	[__TempTable].[__insertdate],
	[__TempTable].[ModifiedBy],
	[__TempTable].[MyReadonly],
	[__TempTable].[Name],
	[__TempTable].[NewInt],
	[__TempTable].[SimpleId]
FROM
	(SELECT  ROW_NUMBER() OVER (ORDER BY [__AUDIT__Simple].__insertdate DESC, [__AUDIT__Simple].__rowid DESC)
	AS __row,
	[__AUDIT__Simple].[__action],
	[__AUDIT__Simple].[__insertdate],
	[__AUDIT__Simple].ModifiedBy,
	[__AUDIT__Simple].[MyReadonly],
	[__AUDIT__Simple].[Name],
	[__AUDIT__Simple].[NewInt],
	[__AUDIT__Simple].[SimpleId]
	FROM 
		[__AUDIT__Simple]
	WHERE
		((@__auditType IS NULL) OR ([__AUDIT__Simple].__action = @__auditType)) AND
	((@__startDate IS NULL) OR ([__AUDIT__Simple].__insertdate >= @__startDate)) AND
	((@__endDate IS NULL) OR ([__AUDIT__Simple].__insertdate < @__endDate)) AND
		(@SimpleId = [__AUDIT__Simple].SimpleId)
	)
	AS [__TempTable]
WHERE
	__row >= (((@__pageOffset-1)*@__recordsPerPage)+1) AND __row <= (@__pageOffset*@__recordsPerPage)

END
return (select COUNT(*) 
	FROM [__AUDIT__Simple] WHERE
		((@__auditType IS NULL) OR ([__AUDIT__Simple].__action = @__auditType)) AND
	((@__startDate IS NULL) OR ([__AUDIT__Simple].__insertdate >= @__startDate)) AND
	((@__endDate IS NULL) OR ([__AUDIT__Simple].__insertdate < @__endDate)) AND
		(@SimpleId = [__AUDIT__Simple].SimpleId)
		)

GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[gen_Simple__AUDIT_SELECT_ALL]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
	drop procedure [dbo].[gen_Simple__AUDIT_SELECT_ALL]
GO

CREATE PROCEDURE [dbo].[gen_Simple__AUDIT_SELECT_ALL]
(
 @__auditType [int] = null,
 @__pageOffset [int] = 1,
 @__recordsPerPage [int] = null,
 @__startDate [datetime] = null,
 @__endDate [datetime] = null
)
AS

SET NOCOUNT ON;

if(@__pageOffset < 1 ) SET @__pageOffset = 1
if((NOT (@__recordsPerPage IS NULL)) AND (@__recordsPerPage < 1 )) SET @__recordsPerPage = 1
IF (@__recordsPerPage IS NULL)
BEGIN
SELECT
	[__AUDIT__Simple].[__action],
	[__AUDIT__Simple].[__insertdate],
	[__AUDIT__Simple].[ModifiedBy],
	[__AUDIT__Simple].[MyReadonly],
	[__AUDIT__Simple].[Name],
	[__AUDIT__Simple].[NewInt],
	[__AUDIT__Simple].[SimpleId]
FROM
	[__AUDIT__Simple]
WHERE
	((@__auditType IS NULL) OR ([__AUDIT__Simple].__action = @__auditType)) AND
	((@__startDate IS NULL) OR ([__AUDIT__Simple].__insertdate >= @__startDate)) AND
	((@__endDate IS NULL) OR ([__AUDIT__Simple].__insertdate < @__endDate))
	ORDER BY [__AUDIT__Simple].__insertdate DESC, [__AUDIT__Simple].__rowid DESC

END
ELSE
BEGIN
SELECT
	[__TempTable].[__action],
	[__TempTable].[__insertdate],
	[__TempTable].[ModifiedBy],
	[__TempTable].[MyReadonly],
	[__TempTable].[Name],
	[__TempTable].[NewInt],
	[__TempTable].[SimpleId]
FROM
	(SELECT  ROW_NUMBER() OVER (ORDER BY [__AUDIT__Simple].__insertdate DESC, [__AUDIT__Simple].__rowid DESC)
	AS __row,
	[__AUDIT__Simple].[__action],
	[__AUDIT__Simple].[__insertdate],
	[__AUDIT__Simple].ModifiedBy,
	[__AUDIT__Simple].[MyReadonly],
	[__AUDIT__Simple].[Name],
	[__AUDIT__Simple].[NewInt],
	[__AUDIT__Simple].[SimpleId]
	FROM 
		[__AUDIT__Simple]
	WHERE
		((@__auditType IS NULL) OR ([__AUDIT__Simple].__action = @__auditType)) AND
	((@__startDate IS NULL) OR ([__AUDIT__Simple].__insertdate >= @__startDate)) AND
	((@__endDate IS NULL) OR ([__AUDIT__Simple].__insertdate < @__endDate))
	)
	AS [__TempTable]
WHERE
	__row >= (((@__pageOffset-1)*@__recordsPerPage)+1) AND __row <= (@__pageOffset*@__recordsPerPage)

END
return (select COUNT(*) 
	FROM [__AUDIT__Simple] WHERE
		((@__auditType IS NULL) OR ([__AUDIT__Simple].__action = @__auditType)) AND
	((@__startDate IS NULL) OR ([__AUDIT__Simple].__insertdate >= @__startDate)) AND
	((@__endDate IS NULL) OR ([__AUDIT__Simple].__insertdate < @__endDate))
		)

GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[gen_Simple_Update]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
	drop procedure [dbo].[gen_Simple_Update]
GO

CREATE PROCEDURE [dbo].[gen_Simple_Update]
(
	@Name [VarChar] (50) = null,
	@NewInt [Int] = 0,
	@ModifiedBy [Varchar] (50) = null,
	@ModifiedDate [DateTime] = null,
	@Original_SimpleId [Int],
	@Original_Timestamp timestamp
)
AS

IF (@ModifiedDate IS NULL)
SET @ModifiedDate = getdate();

SET NOCOUNT ON;
UPDATE 
	[dbo].[Simple] 
SET
	[Name] = @Name,
	[NewInt] = @NewInt,
	[ModifiedBy] = @ModifiedBy,
	[ModifiedDate] = @ModifiedDate

WHERE
	[dbo].[Simple].[SimpleId] = @Original_SimpleId AND
	[dbo].[Simple].[Timestamp] = @Original_Timestamp


if (@@RowCount = 0) return;

SELECT
	[dbo].[Simple].[myreadonly],
	[dbo].[Simple].[name],
	[dbo].[Simple].[newint],
	[dbo].[Simple].[simpleid],
	[dbo].[Simple].[CreatedBy],
	[dbo].[Simple].[CreatedDate],
	[dbo].[Simple].[ModifiedBy],
	[dbo].[Simple].[ModifiedDate],
	[dbo].[Simple].[Timestamp]
FROM 
[dbo].[Simple]
WHERE
	[dbo].[Simple].[SimpleId] = @Original_SimpleId
GO

--This SQL is generated for internal stored procedures for table [TreeNode]

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[gen_TreeNode_Delete]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
	drop procedure [dbo].[gen_TreeNode_Delete]
GO

CREATE PROCEDURE [dbo].[gen_TreeNode_Delete]
(
	@parentTreeNode_TreeNodeId [Int] = null,--Entity Framework Required Parent Keys be passed in: Table 'TreeNode'
	@Original_TreeNodeId [Int]
)
AS
SET NOCOUNT OFF;

DELETE FROM
	[dbo].[TreeNode] 
WHERE 
	[TreeNodeId] = @Original_TreeNodeId;

if (@@RowCount = 0) return;


GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[gen_TreeNode_Insert]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
	drop procedure [dbo].[gen_TreeNode_Insert]
GO

CREATE PROCEDURE [dbo].[gen_TreeNode_Insert]
(
	@FparentTreeNodeId [Int] = null,
	@Name [VarChar] (50),
	@TreeNodeId [Int] = null,
	@CreatedDate [DateTime] = null,
	@CreatedBy [Varchar] (50) = null,
	@ModifiedBy [Varchar] (50) = null

)
AS
SET NOCOUNT OFF;

if (@CreatedDate IS NULL)
SET @CreatedDate = getdate()
DECLARE @ModifiedDate [DateTime]
SET @ModifiedDate = getdate()
IF (@TreeNodeId < 0) SET @TreeNodeId = NULL;
if ((@TreeNodeId IS NULL))
BEGIN
INSERT INTO [dbo].[TreeNode]
(
	[FparentTreeNodeId],
	[Name],
	[CreatedDate],
	[CreatedBy],
	[ModifiedDate],
	[ModifiedBy]
)
VALUES
(
	@FparentTreeNodeId,
	@Name,
	@CreatedDate,
	@CreatedBy,
	@ModifiedDate,
	@ModifiedBy
);

if (@@RowCount = 0) return;

END
ELSE
BEGIN
SET identity_insert [dbo].[TreeNode] on
INSERT INTO [dbo].[TreeNode]
(
	[TreeNodeId],
	[FparentTreeNodeId],
	[Name],
	[CreatedDate],
	[CreatedBy],
	[ModifiedDate],
	[ModifiedBy]
)
VALUES
(
	@TreeNodeId,
	@FparentTreeNodeId,
	@Name,
	@CreatedDate,
	@CreatedBy,
	@ModifiedDate,
	@ModifiedBy
);

if (@@RowCount = 0) return;

SET identity_insert [dbo].[TreeNode] off
END


SELECT 
	[dbo].[TreeNode].[fparenttreenodeid],
	[dbo].[TreeNode].[name],
	[dbo].[TreeNode].[treenodeid],
	[dbo].[TreeNode].[CreatedBy],
	[dbo].[TreeNode].[CreatedDate],
	[dbo].[TreeNode].[ModifiedBy],
	[dbo].[TreeNode].[ModifiedDate],
	[dbo].[TreeNode].[Timestamp]

FROM
[dbo].[TreeNode]
WHERE
	[dbo].[TreeNode].[TreeNodeId] = SCOPE_IDENTITY();
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[gen_TreeNode__AUDIT_SELECT]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
	drop procedure [dbo].[gen_TreeNode__AUDIT_SELECT]
GO

CREATE PROCEDURE [dbo].[gen_TreeNode__AUDIT_SELECT]
(
 @TreeNodeId [Int],
 @__auditType [int] = null,
 @__pageOffset [int] = 1,
 @__recordsPerPage [int] = null,
 @__startDate [datetime] = null,
 @__endDate [datetime] = null
)
AS

SET NOCOUNT ON;

if(@__pageOffset < 1 ) SET @__pageOffset = 1
if((NOT (@__recordsPerPage IS NULL)) AND (@__recordsPerPage < 1 )) SET @__recordsPerPage = 1
IF (@__recordsPerPage IS NULL)
BEGIN
SELECT
	[__AUDIT__TreeNode].[__action],
	[__AUDIT__TreeNode].[__insertdate],
	[__AUDIT__TreeNode].[ModifiedBy],
	[__AUDIT__TreeNode].[FparentTreeNodeId],
	[__AUDIT__TreeNode].[Name],
	[__AUDIT__TreeNode].[TreeNodeId]
FROM
	[__AUDIT__TreeNode]
WHERE
	((@__auditType IS NULL) OR ([__AUDIT__TreeNode].__action = @__auditType)) AND
	((@__startDate IS NULL) OR ([__AUDIT__TreeNode].__insertdate >= @__startDate)) AND
	((@__endDate IS NULL) OR ([__AUDIT__TreeNode].__insertdate < @__endDate)) AND
	(@TreeNodeId = [__AUDIT__TreeNode].TreeNodeId)
	ORDER BY [__AUDIT__TreeNode].__insertdate DESC, [__AUDIT__TreeNode].__rowid DESC

END
ELSE
BEGIN
SELECT
	[__TempTable].[__action],
	[__TempTable].[__insertdate],
	[__TempTable].[ModifiedBy],
	[__TempTable].[FparentTreeNodeId],
	[__TempTable].[Name],
	[__TempTable].[TreeNodeId]
FROM
	(SELECT  ROW_NUMBER() OVER (ORDER BY [__AUDIT__TreeNode].__insertdate DESC, [__AUDIT__TreeNode].__rowid DESC)
	AS __row,
	[__AUDIT__TreeNode].[__action],
	[__AUDIT__TreeNode].[__insertdate],
	[__AUDIT__TreeNode].ModifiedBy,
	[__AUDIT__TreeNode].[FparentTreeNodeId],
	[__AUDIT__TreeNode].[Name],
	[__AUDIT__TreeNode].[TreeNodeId]
	FROM 
		[__AUDIT__TreeNode]
	WHERE
		((@__auditType IS NULL) OR ([__AUDIT__TreeNode].__action = @__auditType)) AND
	((@__startDate IS NULL) OR ([__AUDIT__TreeNode].__insertdate >= @__startDate)) AND
	((@__endDate IS NULL) OR ([__AUDIT__TreeNode].__insertdate < @__endDate)) AND
		(@TreeNodeId = [__AUDIT__TreeNode].TreeNodeId)
	)
	AS [__TempTable]
WHERE
	__row >= (((@__pageOffset-1)*@__recordsPerPage)+1) AND __row <= (@__pageOffset*@__recordsPerPage)

END
return (select COUNT(*) 
	FROM [__AUDIT__TreeNode] WHERE
		((@__auditType IS NULL) OR ([__AUDIT__TreeNode].__action = @__auditType)) AND
	((@__startDate IS NULL) OR ([__AUDIT__TreeNode].__insertdate >= @__startDate)) AND
	((@__endDate IS NULL) OR ([__AUDIT__TreeNode].__insertdate < @__endDate)) AND
		(@TreeNodeId = [__AUDIT__TreeNode].TreeNodeId)
		)

GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[gen_TreeNode__AUDIT_SELECT_ALL]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
	drop procedure [dbo].[gen_TreeNode__AUDIT_SELECT_ALL]
GO

CREATE PROCEDURE [dbo].[gen_TreeNode__AUDIT_SELECT_ALL]
(
 @__auditType [int] = null,
 @__pageOffset [int] = 1,
 @__recordsPerPage [int] = null,
 @__startDate [datetime] = null,
 @__endDate [datetime] = null
)
AS

SET NOCOUNT ON;

if(@__pageOffset < 1 ) SET @__pageOffset = 1
if((NOT (@__recordsPerPage IS NULL)) AND (@__recordsPerPage < 1 )) SET @__recordsPerPage = 1
IF (@__recordsPerPage IS NULL)
BEGIN
SELECT
	[__AUDIT__TreeNode].[__action],
	[__AUDIT__TreeNode].[__insertdate],
	[__AUDIT__TreeNode].[ModifiedBy],
	[__AUDIT__TreeNode].[FparentTreeNodeId],
	[__AUDIT__TreeNode].[Name],
	[__AUDIT__TreeNode].[TreeNodeId]
FROM
	[__AUDIT__TreeNode]
WHERE
	((@__auditType IS NULL) OR ([__AUDIT__TreeNode].__action = @__auditType)) AND
	((@__startDate IS NULL) OR ([__AUDIT__TreeNode].__insertdate >= @__startDate)) AND
	((@__endDate IS NULL) OR ([__AUDIT__TreeNode].__insertdate < @__endDate))
	ORDER BY [__AUDIT__TreeNode].__insertdate DESC, [__AUDIT__TreeNode].__rowid DESC

END
ELSE
BEGIN
SELECT
	[__TempTable].[__action],
	[__TempTable].[__insertdate],
	[__TempTable].[ModifiedBy],
	[__TempTable].[FparentTreeNodeId],
	[__TempTable].[Name],
	[__TempTable].[TreeNodeId]
FROM
	(SELECT  ROW_NUMBER() OVER (ORDER BY [__AUDIT__TreeNode].__insertdate DESC, [__AUDIT__TreeNode].__rowid DESC)
	AS __row,
	[__AUDIT__TreeNode].[__action],
	[__AUDIT__TreeNode].[__insertdate],
	[__AUDIT__TreeNode].ModifiedBy,
	[__AUDIT__TreeNode].[FparentTreeNodeId],
	[__AUDIT__TreeNode].[Name],
	[__AUDIT__TreeNode].[TreeNodeId]
	FROM 
		[__AUDIT__TreeNode]
	WHERE
		((@__auditType IS NULL) OR ([__AUDIT__TreeNode].__action = @__auditType)) AND
	((@__startDate IS NULL) OR ([__AUDIT__TreeNode].__insertdate >= @__startDate)) AND
	((@__endDate IS NULL) OR ([__AUDIT__TreeNode].__insertdate < @__endDate))
	)
	AS [__TempTable]
WHERE
	__row >= (((@__pageOffset-1)*@__recordsPerPage)+1) AND __row <= (@__pageOffset*@__recordsPerPage)

END
return (select COUNT(*) 
	FROM [__AUDIT__TreeNode] WHERE
		((@__auditType IS NULL) OR ([__AUDIT__TreeNode].__action = @__auditType)) AND
	((@__startDate IS NULL) OR ([__AUDIT__TreeNode].__insertdate >= @__startDate)) AND
	((@__endDate IS NULL) OR ([__AUDIT__TreeNode].__insertdate < @__endDate))
		)

GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[gen_TreeNode_Update]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
	drop procedure [dbo].[gen_TreeNode_Update]
GO

CREATE PROCEDURE [dbo].[gen_TreeNode_Update]
(
	@FparentTreeNodeId [Int] = null,
	@Name [VarChar] (50),
	@ModifiedBy [Varchar] (50) = null,
	@ModifiedDate [DateTime] = null,
	@Original_TreeNodeId [Int],
	@Original_Timestamp timestamp
)
AS

IF (@ModifiedDate IS NULL)
SET @ModifiedDate = getdate();

SET NOCOUNT ON;
UPDATE 
	[dbo].[TreeNode] 
SET
	[FparentTreeNodeId] = @FparentTreeNodeId,
	[Name] = @Name,
	[ModifiedBy] = @ModifiedBy,
	[ModifiedDate] = @ModifiedDate

WHERE
	[dbo].[TreeNode].[TreeNodeId] = @Original_TreeNodeId AND
	[dbo].[TreeNode].[Timestamp] = @Original_Timestamp


if (@@RowCount = 0) return;

SELECT
	[dbo].[TreeNode].[fparenttreenodeid],
	[dbo].[TreeNode].[name],
	[dbo].[TreeNode].[treenodeid],
	[dbo].[TreeNode].[CreatedBy],
	[dbo].[TreeNode].[CreatedDate],
	[dbo].[TreeNode].[ModifiedBy],
	[dbo].[TreeNode].[ModifiedDate],
	[dbo].[TreeNode].[Timestamp]
FROM 
[dbo].[TreeNode]
WHERE
	[dbo].[TreeNode].[TreeNodeId] = @Original_TreeNodeId
GO

